简体   繁体   English

在子类上使用Toplevel的Tkinter命令问题

[英]Tkinter command problems using Toplevel on child class

Trying to write a simple gui which has multiple child classes but the toplevel is not recognizing the attributes that I set with the command on a button so that it prints anything the user puts in the box. 尝试编写具有多个子类但上层的gui的简单gui不能识别我在按钮上用命令设置的属性,以便它打印用户放入框中的所有内容。 i have no clue why it isn't working on the child class 我不知道为什么它不能在儿童班上工作

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

You're setting command l.enter it should be command = self.enter 您正在设置command l.enter它应该是command = self.enter

To pass the widget (Toplevel) command = lambda: self.enter(l) or pass l.entry and then you can just call l.get() 要传递小部件( command = lambda: self.enter(l)command = lambda: self.enter(l)或传递l.entry ,则只需调用l.get()

Also I think you want dns = l.entry.get() to print the entry widgets text 另外,我认为您希望dns = l.entry.get()打印条目小部件文本

You should add self at the beginning of your enter function as an argument to call self.enter with an additional arguement, or you could just refactor this outside of your class. 您应该在输入函数的开头添加self作为参数,以额外的争论来调用self.enter,或者可以在类之外重构它。

You have many issues with your code: 您的代码有很多问题:

  1. You have a typo in command l.enter . 您在command l.enter有错字。 Correct it: command = self.enter 更正它: command = self.enter
  2. The notations l.entry and l.button are wrong because this means in Python that you have already created attributes called entry and button which you are now allowed to access with the dot notation as you did. l.entryl.button表示法是错误的,因为这意味着在Python中您已经创建了名为entry和button的属性,现在可以像以前一样使用点符号来访问它们。 You should rename these to something else. 您应该将它们重命名为其他名称。 For example: l_entry and l_entry . 例如: l_entryl_entry
  3. Consequently to 2. , in enter() you will need to modify l.entry.get() to l_entry.get() 结果是2 ,在enter()您需要将l.entry.get()修改为l_entry.get()
  4. To be able to use l_entry.get() in enter() method, you will need to write self.l_entry = tk.Button(self.l, text="search mx", command= self.enter) in create_window() and then in enter() write dns =(self.l_entry.get()) instead of dns =(l_entry.get()) . 为了能够在enter()方法中使用l_entry.get() ,您将需要在l_entry.get()中编写self.l_entry = tk.Button(self.l, text="search mx", command= self.enter) create_window()然后在enter()写入dns =(self.l_entry.get())而不是dns =(l_entry.get())

Your program becomes now like this: 您的程序现在变成这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM