Tkinter command problems using Toplevel on child class Tkinter command problems using Toplevel on child class tkinter tkinter

Tkinter command problems using Toplevel on child class


You're setting command l.enter it should be 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()

Also I think you want dns = l.entry.get() to print the entry widgets text

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.


You have many issues with your code:

  1. You have a typo in command l.enter. Correct it: 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. You should rename these to something else. For example: l_entry and l_entry.
  3. Consequently to 2., in enter() you will need to modify l.entry.get() to 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()).

Your program becomes now like this:

from Tkinter import *import Tkinter as tkimport tkSimpleDialogimport tkMessageBoxclass 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()