我正在将Entry窗口小部件StringVal传递给一个函数,该函数在另一个模块中查找您键入的内容。即a1 =123456。当我在Entry小部件中键入“ a1”时,我的函数仅返回“ a1”,而不返回其对应的值“ 123456”。我不知道如何获取def用户函数来将标签配置为123456而不是a1。
from tkinter import *
class App(Frame):
def __init__(self, master):
super(App, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
a1 = 123456
a2 = 456789
a3 = 789456
self.Userin = StringVar()
self.user_input = Entry(self, insertwidth=0, width=30,
textvariable=self.Userin)
self.user_input.grid(row=2)
self.user_input.insert(0, "0")
self.bttn1 = Button(self, padx = 15, pady = 15, text = "Call A1",
command = lambda: self.getdata(a1))
self.bttn1.grid(row = 0, column = 0)
self.bttn2 = Button(self, padx=15, pady=15, text="Call your entry",
command=lambda: self.userd(self.Userin.get()))
self.bttn2.grid(row=0, column=1)
self.label = Label(self,bg = "white", padx = 35, pady = 25, text = "***")
self.label.grid(row = 1)
def userd(self,xxx):
self.label.configure(text = xxx)
def getdata(self,row):
self.label.configure(text = row)
root = Tk()
root.geometry("300x150")
App(root)
root.mainloop()
答案 0 :(得分:1)
您的代码可以正常工作。您的函数返回“ a1”,因为您从不编写其他任何代码。
添加如下条件:
from tkinter import *
class App(Frame):
def __init__(self, master):
super(App, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.a1 = 123456
self.a2 = 456789
self.a3 = 789456
self.Userin = StringVar()
self.user_input = Entry(self, insertwidth=0, width=30,
textvariable=self.Userin)
self.user_input.grid(row=2)
self.user_input.insert(0, "0")
self.bttn1 = Button(self, padx = 15, pady = 15, text = "Call A1",
command = lambda: self.getdata(self.a1))
self.bttn1.grid(row = 0, column = 0)
self.bttn2 = Button(self, padx=15, pady=15, text="Call your entry",
command=lambda: self.userd(self.Userin.get()))
self.bttn2.grid(row=0, column=1)
self.label = Label(self,bg = "white", padx = 35, pady = 25, text = "***")
self.label.grid(row = 1)
def userd(self,xxx):
if xxx == "a1":
self.label.configure(text = self.a1)
elif xxx == "a2":
self.label.configure(text = self.a2)
elif xxx == "a3":
self.label.configure(text = self.a3)
def getdata(self,row):
self.label.configure(text = row)
root = Tk()
root.geometry("300x150")
App(root)
root.mainloop()