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.
JavaScript
x
34
34
1
import tkinter as tk
2
import pywhatkit
3
import pyautogui
4
5
6
class automation(tk.Tk):
7
def __init__(self, *args, **kwargs):
8
tk.Tk.__init__(self, *args, **kwargs)
9
self.geometry("842x510")
10
self.configure(bg="#ffffff")
11
self.resizable(False, False)
12
container = tk.Frame(self, background="#ffffff")
13
container.grid(row=0, column=0, sticky='nesw')
14
15
container.pack(side="top", fill="both")
16
17
self.frames = {}
18
19
for F in (mainwindow, SecondWindow):
20
page_name = F.__name__
21
frame = F(parent=container, controller=self)
22
frame.grid(row=0, column=0, sticky="nsew")
23
self.frames[page_name] = frame
24
25
self.show_frame('mainwindow')
26
27
def show_frame(self, page_name):
28
frame = self.frames[page_name]
29
frame.tkraise()
30
31
32
def btn_clicked():
33
print("Button Clicked")
34
This is my first window code
JavaScript
1
34
34
1
class mainwindow(tk.Frame):
2
def __init__(self, parent, controller):
3
tk.Frame.__init__(self, parent)
4
self.frame = tk.Frame(self, background="#ffffff")
5
self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=391, bd=0,
6
highlightthickness=0, relief="ridge")
7
self.canvas.place(x=0, y=0)
8
self.canvas.pack(side='left')
9
self.background_img = tk.PhotoImage(file="background.png")
10
self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")
11
12
self.img1 = tk.PhotoImage(file="img1.png")
13
self.b1 = tk.Button(self, image=self.img1, borderwidth=0, highlightthickness=0,
14
command=lambda: controller.show_frame(SecondWindow),
15
relief="flat")
16
17
self.b1.place(x=392, y=100, width=348, height=62)
18
self.b1.pack(padx=10, pady=125)
19
20
# creating third button
21
22
self.img3 = tk.PhotoImage(file="img3.png")
23
self.b3 = tk.Button(self, image=self.img3, borderwidth=0, highlightthickness=0,
24
command=btn_clicked, relief="flat")
25
26
self.b3.place(x=392, y=200, width=348, height=62)
27
self.b3.pack(padx=10, pady=0)
28
29
30
def send(num, msg, hour, minute):
31
pywhatkit.sendwhatmsg(f"+91{num}", msg, hour, minute, 36)
32
pyautogui.click()
33
pyautogui.press("enter")
34
This is my Second window code
JavaScript
1
72
72
1
class SecondWindow(tk.Frame):
2
3
def __init__(self, parent, controller):
4
tk.Frame.__init__(self, parent, controller)
5
6
a = tk.StringVar()
7
b = tk.StringVar()
8
c = tk.IntVar()
9
d = tk.IntVar()
10
11
self.frame = tk.Frame(self, background="#ffffff")
12
self.canvas = tk.Canvas(self, bg="#ffffff", height=510, width=842, bd=0,
13
highlightthickness=0, relief="ridge")
14
self.canvas.place(x=0, y=0)
15
self.canvas.pack(side='left')
16
self.canvas.pack()
17
# set image in background
18
self.background_img = tk.PhotoImage(file="background2.png")
19
self.canvas.create_image(0, 0, image=self.background_img, anchor="nw")
20
21
# enter a number
22
self.entry1_img = tk.PhotoImage(file="textBox1.png")
23
self.canvas.create_image(570, 112, image=self.entry1_img)
24
25
self.entry1 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=a)
26
self.entry1.place(x=423, y=90, width=280, height=45)
27
28
# enter a massage
29
30
self.entry2_img = tk.PhotoImage(file="textBox2.png")
31
self.canvas.create_image(570, 225, image=self.entry2_img)
32
33
self.entry2 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=b)
34
35
self.entry2.place(x=423, y=203, width=280, height=45)
36
37
# enter a time -> Hour
38
self.entry3_img = tk.PhotoImage(file="textBox3.png")
39
self.canvas.create_image(470, 329, image=self.entry3_img)
40
41
self.entry3 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=c)
42
43
self.entry3.place(x=423, y=312, width=80.0, height=35)
44
45
# minute
46
self.entry4_img = tk.PhotoImage(file="textBox4.png")
47
self.canvas.create_image(676, 329, image=self.entry4_img)
48
49
self.entry4 = tk.Entry(bd=0, bg="#c4c4c4", highlightthickness=0, textvariable=d)
50
51
self.entry4.place(x=630, y=312, width=80.0, height=35)
52
53
# Go home
54
self.img4 = tk.PhotoImage(file="img4.png")
55
self.b4 = tk.Button(image=self.img4, borderwidth=0, highlightthickness=0,
56
command=lambda: controller.show_frame(mainwindow), relief="flat")
57
58
self.b4.place(x=418, y=400, width=100, height=37)
59
60
# Send message
61
self.img5 = tk.PhotoImage(file="img5.png")
62
self.b5 = tk.Button(image=self.img5, borderwidth=0, highlightthickness=0,
63
command=lambda: send(a.get(), b.get(), c.get(), d.get()),
64
relief="flat")
65
66
self.b5.place(x=642, y=400, width=100, height=37)
67
68
69
if __name__ == '__main__':
70
app = automation()
71
app.mainloop()
72
I received this Error.
JavaScript
1
15
15
1
Traceback (most recent call last):
2
File "E:myproject1mix.py", line 140, in <module>
3
app = automation()
4
File "E:myproject1mix.py", line 21, in __init__
5
frame = F(parent=container, controller=self)
6
File "E:myproject1mix.py", line 75, in __init__
7
tk.Frame.__init__(self, parent, controller)
8
File "C:UsersHPAppDataLocalProgramsPythonPython310libtkinter__init__.py", line
9
3153, in __init__
10
Widget.__init__(self, master, 'frame', cnf, {}, extra)
11
File "C:UsersHPAppDataLocalProgramsPythonPython310libtkinter__init__.py", line
12
2601, in __init__
13
self.tk.call(
14
_tkinter.TclError: unknown option "-menu"
15
please help me to resolve this error.
Advertisement
Answer
You have passed extra argument controller
to tk.Frame.__init__(...)
:
JavaScript
1
8
1
2
class SecondWindow(tk.Frame):
3
4
def __init__(self, parent, controller):
5
#tk.Frame.__init__(self, parent, controller)
6
tk.Frame.__init__(self, parent) # removed controller argument
7
8