在类中使用Button命令从另一个类调用函数

2024-10-02 02:44:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序是使用第一类中的一个按钮来触发第二类中的函数。但是,当按钮被按下时,它要求我包含一个自参数,那么我如何通过使用静态方法建议的表单(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()

预期结果 当用户按下按钮时,条目将得到文本并显示在终端中。在


Tags: textselfmasterinitmaindefrootcontent
2条回答

解决了。我做了一些事情来让您的代码按照您想要的规范工作:

  1. 在printSecondLine()方法上方添加了@staticmethod。在
  2. 从printsecondaline()参数列表中删除了self。在
  3. 使printSecondLine()接受名为value的参数。在
  4. 删除并重新编写printSecondLine()方法的主体,使其按预期运行。在
  5. 在按钮配置中使用了lambda表达式,以便可以轻松传递数据。在
  6. 将tvar作为lambda表达式的参数,因为这是传递给printsecondaline()的参数
  7. 将tvar和进入代码放在按钮代码上方。换个顺序也许可以。但我认为最好把tvar放在上面,因为在你从按钮访问它之前先声明它。在

不管怎样,它是有效的。在entry中输入数据,单击按钮,数据就会进入控制台。您可能需要编写代码,以便在单击按钮后清除输入字段。但这是另一个话题,不难做到。在

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()

when creating buttons you cannot set command= to function with parantheses '()' unless using lambda like this.

self.traceButton = Button(self.main_inner_frame,text="Trace",
command= lambda: Second.printSecondLine())

但这也应该起作用:

^{pr2}$

相关问题 更多 >

    热门问题