I am following the tutorial (more or less) at this site. I am able to copy and paste the code from the tutorial and modify it with the Python 3 module names (Tkinter
becomes tkinter
, Queue
becomes queue
, etc.) and the code runs fine.
However, my code creates widgets in a method.
import tk inter as tk
class ClientUI:
def __init__(self, master, queue, send_command):
self.queue = queue
self.send_command = send_command
self.master = master
menu_bar = tk.Menu(master)
menu_file = tk.Menu(menu_bar, tearoff=0)
menu_file.add_command(label="Preferences", command=self.show_preferences)
menu_file.add_command(label="Quit", command=master.destroy)
menu_bar.add_cascade(label="Client", menu=menu_file)
master.config(menu=menu_bar)
master.grid()
self.create_widgets()
...
def create_widgets(self):
output = tk.Text(self.master, state=tk.DISABLED, name="output")
output.grid(row=0)
input_area = tk.Entry(self, name="input")
input_area.bind("<Return>", self.send_command)
input_area.focus()
input_area.grid(row=1, sticky=tk.W+tk.E)
...
class Client:
def __init__(self, master):
# super(Client, self).__init__()
self.master = master
self.queue = Queue()
self.ui = ClientUI(master, self.queue, self.send)
...
...
root = tk.Tk()
client = Client(root)
root.mainloop()
When I try to run this I get the error
output = tk.Text(self.master, state=self.master.DISABLED, name="output")
AttributeError: 'ClientUI' object has no attribute 'tk'
I don't understand why he sets root = tk.Tk()
and how I can access the tkinter
module to create widgets.