我的程序正在使用First类中的按钮来触发Second类中的函数。但是,当按下按钮时,它要求我包含一个自变量,因此如何通过使用此处建议的静态方法建议的形式(querySelector()
)或使用其他替代方法来解决此问题,从而调用其他函数。 >
如果您可以在第二课中向我建议如何使用StringVar,那么对我来说,这将更加容易。
我认为应该修改的唯一代码部分:
self.traceButton=Button(self.main_inner_frame,text="Trace",command=Second.printSecondLine())
第二类代码:
class Second(First):
# @staticmethod
def __init__(self,master):
super().__init__(master)
def printSecondLine(self):
content = self.traceEntry.get()
print(content)
如果您需要完整代码:
from tkinter import *
class First(Frame):
def __init__(self,master):
super().__init__()
master.minsize(width=755, height=520)
master.maxsize(width=755, height=520)
Grid.config(self)
# Run all function in [First Class]
self.widget_size()
#self.getTEXT()
def widget_size(self):
# Define a small frame for it
self.main_inner_frame = LabelFrame(self,text="Tracing Method",height= 120,width =355)
self.main_inner_frame.grid(row= 0, column=0)
self.main_inner_frame.grid_propagate(0)
# Create a button for it [When trace click it will show the text in the entry on terminal]
self.traceButton = Button(self.main_inner_frame, text="Trace",command=Second.printSecondLine())
self.traceButton.grid(row = 0, column = 1, sticky = W)
# Create a entry box for the user
# use a string variable tvar
tvar = StringVar()
self.traceEntry = Entry(self.main_inner_frame,textvariable=tvar,width=30)
self.traceEntry.grid(row = 0, column = 2, sticky = W)
# def getTEXT(self):
# content = self.traceEntry.get()
# print(content)
class Second(First):
# @staticmethod
def __init__(self,master):
super().__init__(master)
def printSecondLine(self):
content = self.traceEntry.get()
print(content)
return content
if __name__ == '__main__':
root = Tk()
root.title("Good System")
TIF = First(root)
root.mainloop()
预期结果 当用户按下按钮时,条目将获取文本并显示在终端中。
答案 0 :(得分:1)
已解决。。我做了一些事情,以使您的代码根据您期望的规格工作:
无论如何,它有效。输入数据后,单击按钮,数据进入控制台。您可能想要编写单击按钮后清除“输入”字段的代码。但这是一个不同的话题,并不难。
from tkinter import *
class First(Frame):
def __init__(self,master):
super().__init__()
master.minsize(width=755, height=520)
master.maxsize(width=755, height=520)
Grid.config(self)
# Run all function in [First Class]
self.widget_size()
def widget_size(self):
# Define a small frame for it
self.main_inner_frame = LabelFrame(self,text="Tracing Method",height= 120,width =355)
self.main_inner_frame.grid(row= 0, column=0)
self.main_inner_frame.grid_propagate(0)
# Create a entry box for the user
# use a string variable tvar
tvar = StringVar()
self.traceEntry = Entry(self.main_inner_frame,textvariable=tvar,width=30)
self.traceEntry.grid(row = 0, column = 2, sticky = W)
# Create a button for it [When trace click it will show the text in the entry on terminal]
self.traceButton = Button(self.main_inner_frame, text="Trace",command=lambda: Second.printSecondLine(tvar))
self.traceButton.grid(row = 0, column = 1, sticky = W)
class Second(First):
@staticmethod
def __init__(self,master):
super().__init__(master)
@staticmethod
def printSecondLine(value):
print(value.get())
if __name__ == '__main__':
root = Tk()
root.title("Good System")
TIF = First(root)
root.mainloop()
答案 1 :(得分:0)
创建按钮时,除非使用lambda,否则无法将command =设置为带有括号'()'的功能。
self.traceButton = Button(self.main_inner_frame, text="Trace",command=Second.printSecondLine)
但这应该也可以工作:
{{1}}