【问题标题】:Tkinter command problems using Toplevel on child class在子类上使用 Toplevel 的 Tkinter 命令问题
【发布时间】:2016-04-15 00:54:50
【问题描述】:

尝试编写一个具有多个子类的简单 gui,但顶层无法识别我在按钮上使用命令设置的属性,因此它会打印用户放入框中的任何内容。我不知道为什么它不适用于子类

   from Tkinter import *
    import Tkinter as tk
    import tkSimpleDialog
    import tkMessageBox

    class MainWindow(tk.Frame):

        def __init__(self, *args, **kwargs):
            tk.Frame.__init__(self, *args, **kwargs)

            self.button = tk.Button(self, text="mx lookup",command=self.create_window)
            self.button.pack(side="left")





        def create_window(self):

            l = tk.Toplevel(self)
            l.entry = tk.Entry(l)
            l.button = tk.Button(l, text="search mx", command l.enter)

            l.entry.pack()
            l.button.pack()
        def enter(l):
            dns =(l.entry.get())
                  print(dns)






    if __name__ == "__main__":
        root = tk.Tk()
        main = MainWindow(root)
        main.pack(side="top", fill="both", expand=True)

        root.mainloop()

【问题讨论】:

    标签: python tkinter command


    【解决方案1】:

    你设置command l.enter应该是command = self.enter

    要传递小部件(Toplevel)command = lambda: self.enter(l) 或传递l.entry 然后你可以调用l.get()

    我还认为您希望 dns = l.entry.get() 打印条目小部件文本

    你应该在你的 enter 函数的开头添加 self 作为一个参数来调用 self.enter 并附加一个参数,或者你可以在你的类之外重构它。

    【讨论】:

    • 它仍然在第 22 行出现错误,说它只需要 1 个参数,但给出了 2 个任何解决方案
    【解决方案2】:

    您的代码有很多问题:

    1. command l.enter 中有错字。更正它:command = self.enter
    2. 符号 l.entryl.button 是错误的,因为这意味着在 Python 中您已经创建了名为 entry 和 button 的属性,现在您可以像以前一样使用点符号访问它们。您应该将它们重命名为其他名称。例如:l_entryl_entry
    3. 因此2.,在enter() 中,您需要将l.entry.get() 修改为l_entry.get()
    4. 为了能够在 enter() 方法中使用l_entry.get(),您需要在create_window() 中写入self.l_entry = tk.Button(self.l, text="search mx", command= self.enter),然后在enter() 中写入dns =(self.l_entry.get()) 而不是dns =(l_entry.get())

    你的程序现在变成这样:

    from Tkinter import *
    import Tkinter as tk
    import tkSimpleDialog
    import tkMessageBox
    
    class MainWindow(tk.Frame):
       def __init__(self, *args, **kwargs):
           tk.Frame.__init__(self, *args, **kwargs)
           self.button = tk.Button(self, text="mx lookup",command=self.create_window)
           self.button.pack(side="left")
    
       def create_window(self):
           self.l = tk.Toplevel(self)              
           self.l_entry = tk.Entry(self.l)
           self.l_button = tk.Button(self.l, text="search mx", command= self.enter)    
           self.l_entry.pack()
           self.l_button.pack()
    
       def enter(self):
           dns =(self.l_entry.get())
           print(dns)
    
    if __name__ == "__main__":
       root = tk.Tk()
       main = MainWindow(root)
       main.pack(side="top", fill="both", expand=True)
       root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2017-08-14
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 2017-12-26
      相关资源
      最近更新 更多