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:
- You have a typo in
command l.enter
. Correct it:command = self.enter
- The notations
l.entry
andl.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
andl_entry
. - Consequently to 2., in
enter()
you will need to modifyl.entry.get()
tol_entry.get()
- To be able to use
l_entry.get()
in enter() method, you will need to writeself.l_entry = tk.Button(self.l, text="search mx", command= self.enter)
increate_window()
and then inenter()
writedns =(self.l_entry.get())
instead ofdns =(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()