我的程序是使用第一类中的一个按钮来触发第二类中的函数。但是,当按钮被按下时,它要求我包含一个自参数,那么我如何通过使用静态方法建议的表单(Python 3 Tkinter - How to call a function from another class)或使用其他替代方法来调用另一个函数呢。在
如果您能建议我如何在第二类中使用StringVar,那对我来说会更容易,此致。在
我认为应该修改的代码部分:
self.traceButton=Button(self.main_inner_frame,text="Trace",command=Second.printSecondLine())
第二类代码:
^{pr2}$如果您需要完整的代码:
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()
预期结果 当用户按下按钮时,条目将得到文本并显示在终端中。在
解决了。我做了一些事情来让您的代码按照您想要的规范工作:
不管怎样,它是有效的。在entry中输入数据,单击按钮,数据就会进入控制台。您可能需要编写代码,以便在单击按钮后清除输入字段。但这是另一个话题,不难做到。在
但这也应该起作用:
^{pr2}$相关问题 更多 >
编程相关推荐