在tkinter中有一种方法可以创建一个函数,它将参数作为字符串返回吗?

huangapple go评论75阅读模式
英文:

Is there a way to have a function that returns the argument as a string in tkinter?

问题

导入 customtkinter 作为 ctk
导入 tkinter 作为 tk
从 tkinter 导入 ttk

定义 get_var(input):
    input.get()

创建 entry_var 作为 ctk.StringVar()
创建 entry_box 作为 ctk.CTkEntry(
    root,
    textvariable = entry_var
    ).grid(row = 0, column = 0)

创建 submit_button 作为 ctk.CTkButton(
    root,
    text = "Submit entry",
    command = get_var(entry_var)
    ).grid(row = 1, column = 0)
英文:
import customtkinter as ctk
import tkinter as tk
from tkinter import ttk

def get_var(input):
    input.get()

entry_var = ctk.StringVar()
entry_box = ctk.CTkEntry(
    root,
    textvariable = entry_var
    ).grid(row = 0, column = 0)

submit_button = ctk.CTkButton(
    root,
    text = "Submit entry",
    command = get_var(entry_var)
    ).grid(row = 1, column = 0)

.get() isn't recognized unless I use a stringvar from tkinter. I'm clueless as to why.
Could anyone educate me and help me understand?

Is there no way to accept the argument as the tk.stringvar? or am I just doing something wrong?

答案1

得分: 0

I'm not 100% familiar with CustomTkinter, but in vanilla tkinter, the geometry manager methods grid, pack, and place all return None. Your widgets are evaluating to None, so they won't respond to get(). Also, input is a reserved word for the input function - you should use another name here.

You also need to modify the command argument for submit_button. As written, it's going to call get_var immediately, which isn't what you want. You can avoid this by using a lambda or by modifying get_var to just call entry_var.get() directly (this is my preferred approach, since you don't have to pass in the entry_box widget).

def get_var():  # no need to pass in the widget
    return entry_var.get()  # just call 'get' directly
    # you'll probably want to do something with the value other than just 'return'
    # it, but this is an example...

entry_var = ctk.StringVar()
entry_box = ctk.CTkEntry(
    root,
    textvariable=entry_var
)
entry_box.grid(row=0, column=0)  # do this on a separate line

submit_button = ctk.CTkButton(
    root,
    text="Submit entry",
    command=get_var,  # pass the function by name here, no parens ()
)
submit_button.grid(row=1, column=0)  # do this on a separate line
英文:

I'm not 100% familiar with CustomTkinter, but it vanilla tkinter the geometry manager methods grid, pack, and place all return None. As written, your widgets are evaluating to None and therefore won't respond to get(). Also, input is a reserved word for the input function - you should use another name here.

You also need to modify the command argument for submit_button. As written, it's going to call get_var immediately, which isn't what you want. You can avoid this by using a lambda or by modifying get_var to just call entry_var.get() directly (this is my preferred approach, since you don't have to pass in the entry_box widget).

def get_var():  # no need to pass in the widget
    return entry_var.get()  # just call 'get' directly
    # you'll probably want to do something with the value other than just 'return'
    # it, but this is an example...


entry_var = ctk.StringVar()
entry_box = ctk.CTkEntry(
    root,
    textvariable=entry_var
)
entry_box.grid(row = 0, column = 0)  # do this on a separate line

submit_button = ctk.CTkButton(
    root,
    text="Submit entry",
    command=get_var,  # pass the function by name here, no parens ()
)
submit_button.grid(row = 1, column = 0)  # do this on a separate line

huangapple
  • 本文由 发表于 2023年5月15日 14:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定