Why am I receiving a error when trying to use tkinter to make multiple windows gui?
I am trying to create multiple window GUI application but can't open window. i received an error. when we run both window code separately it's run. I don't know why this error occurs. can anyone resolve this problem. and help me to run multiple frames in one window.
import tkinter as tk
import pywhatkit
import pyautogui
class automation(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.geometry("842x510")
self.configure(bg="#ffffff")
self.resizable(False, False)
container = tk.Frame(self, background="#ffffff")
container.grid(row=0, column=0, sticky='nesw')
container.pack(side="top", fill="both")
self.frames = {}
for F in (mainwindow, SecondWindow):
page_name = F.__name__
frame = F(parent=container, controller=self)
frame.grid(row=0, column=0, sticky="nsew")
self.frames[page_name] = frame
self.show_frame('mainwindow')
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def btn_clicked():
print("Button Clicked")
This is my first window code
class mainwindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.frame = tk.Frame(self, background="#ffffff")
self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=391, bd=0,
highlightthickness=0, relief="ridge")
self.canvas.place(x=0, y=0)
self.canvas.pack(side='left')
self.background_img = tk.PhotoImage(file="background.png")
self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")
self.img1 = tk.PhotoImage(file="img1.png")
self.b1 = tk.Button(self, image=self.img1, borderwidth=0, highlightthickness=0,
command=lambda: controller.show_frame(SecondWindow),
relief="flat")
self.b1.place(x=392, y=100, width=348, height=62)
self.b1.pack(padx=10, pady=125)
# creating third button
self.img3 = tk.PhotoImage(file="img3.png")
self.b3 = tk.Button(self, image=self.img3, borderwidth=0, highlightthickness=0,
command=btn_clicked, relief="flat")
self.b3.place(x=392, y=200, width=348, height=62)
self.b3.pack(padx=10, pady=0)
def send(num, msg, hour, minute):
pywhatkit.sendwhatmsg(f"+91{num}", msg, hour, minute, 36)
pyautogui.click()
pyautogui.press("enter")
This is my Second window code
class SecondWindow(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent, controller)
a = tk.StringVar()
b = tk.StringVar()
c = tk.IntVar()
d = tk.IntVar()
self.frame = tk.Frame(self, background="#ffffff")
self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=842, bd=0,
highlightthickness=0, relief="ridge")
self.canvas.place(x=0, y=0)
self.canvas.pack(side='left')
self.canvas.pack()
# set image in background
self.background_img = tk.PhotoImage(file="background2.png")
self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")
# enter a number
self.entry1_img = tk.PhotoImage(file="textBox1.png")
self.canvas.create_image(570, 112, image=self.entry1_img)
self.entry1 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=a)
self.entry1.place(x=423, y=90, width=280, height=45)
# enter a massage
self.entry2_img = tk.PhotoImage(file="textBox2.png")
self.canvas.create_image(570, 225, image=self.entry2_img)
self.entry2 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=b)
self.entry2.place(x=423, y=203, width=280, height=45)
# enter a time -> Hour
self.entry3_img = tk.PhotoImage(file="textBox3.png")
self.canvas.create_image(470, 329, image=self.entry3_img)
self.entry3 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=c)
self.entry3.place(x=423, y=312, width=80.0, height=35)
# minute
self.entry4_img = tk.PhotoImage(file="textBox4.png")
self.canvas.create_image(676, 329, image=self.entry4_img)
self.entry4 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=d)
self.entry4.place(x=630, y=312, width=80.0, height=35)
# Go home
self.img4 = tk.PhotoImage(file="img4.png")
self.b4 = tk.Button(image=self.img4, borderwidth=0, highlightthickness=0,
command=lambda: controller.show_frame(mainwindow), relief="flat")
self.b4.place(x=418, y=400, width=100, height=37)
# Send message
self.img5 = tk.PhotoImage(file="img5.png")
self.b5 = tk.Button(image=self.img5, borderwidth=0, highlightthickness=0,
command=lambda: send(a.get(), b.get(), c.get(), d.get()),
relief="flat")
self.b5.place(x=642, y=400, width=100, height=37)
if __name__ == '__main__':
app = automation()
app.mainloop()
I received this Error.
Traceback (most recent call last):
File "E:\myproject1\mix.py", line 140, in <module>
app = automation()
File "E:\myproject1\mix.py", line 21, in __init__
frame = F(parent=container, controller=self)
File "E:\myproject1\mix.py", line 75, in __init__
tk.Frame.__init__(self, parent, controller)
File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line
3153, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Users\HP\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line
2601, in __init__
self.tk.call(
_tkinter.TclError: unknown option "-menu"
please help me to resolve this error.
Solution 1:
You have passed extra argument controller
to tk.Frame.__init__(...)
:
...
class SecondWindow(tk.Frame):
def __init__(self, parent, controller):
#tk.Frame.__init__(self, parent, controller)
tk.Frame.__init__(self, parent) # removed controller argument
...