I am trying to create GUI for some application. Below is my code. Here whenever i click + icon that creates new window,
import tkinter as tk
from tkinter import *
class MainApplication(tk.Tk):
def __init__(self):
super().__init__()
self.overrideredirect(True)
# self.iconbitmap("D:\\kural\\PROJECTS ZETA\\ESP32\\Python\\ICON\\python-logo.png")
self.title_bar = tk.Frame(self, bg='DodgerBlue2', relief='raised', bd=2)
self.title_bar.pack(fill="x")
self.title = tk.Label(self.title_bar,
text="ESP32 Temperature & Humidity Measurement",
bg="DodgerBlue2",
fg="white",
font='Helvetica 20 bold')
self.title.pack(side=tk.LEFT)
self.close_button = tk.Button(self.title_bar,
text=" X ",
bg="DodgerBlue2",
fg="white",
font='Helvetica 20 bold',
relief=FLAT,
command=self.destroy)
self.close_button.pack(side=tk.RIGHT)
self.minimize_button = tk.Button(self.title_bar,
text=" ~ ",
bg="DodgerBlue2",
fg="white",
relief=FLAT,
font='Helvetica 20 bold')
self.minimize_button.pack(side=tk.RIGHT)
self.status_frame = tk.Frame(self,
bd=1,
bg="DodgerBlue2",
relief=tk.SUNKEN)
self.status_frame.pack(side=tk.BOTTOM, fill=tk.X)
self.status_var = tk.StringVar()
self.button1 = tk.Button(self.status_frame,
text="+",
bg="DodgerBlue2",
fg="white",
font='Helvetica 25 bold',
relief=FLAT,
command=self.add_device)
self.button1.pack(side=tk.RIGHT, padx=5)
def add_device(self):
new_window = tk.Toplevel()
new_window.state("zoomed")
label = tk.Label(new_window, text="hello")
label.pack()
if __name__ == "__main__":
app = MainApplication()
app.state("zoomed")
app.mainloop()
here self.add_device function is the code for to create new window.
my problem is new window created but the label widget doesn't appear in second window and also the second window doesn't maximized.
Can anyone suggest me what is the problem in this code?
I hope this helps at least a little, I know tkinter can be a pain.
I've done some experimenting and I got the error message:
I can't figure out why python is trying to call the Label object when you're declaring a new TopLevel class. I did change this line of code (line 9) so I could read the errors and change the code:
To this:
It just makes it so window manager has control over the window this way I could easily make it maximize[enter image description here][1].
I also changed your add_device(self) function to see what line was throwing the error. From:
To:
This threw the same error as running the whole function, so the problem has to be the creation of the new TopLevel. The new TopLevel object is never fully created, that's why you can't make create a Label inside of it, I couldn't even change it's geometry or title. Even a blank new TopLevel should create a new window that can be edited.
Sorry I couldn't help more, I have a pretty surface level knowledge of how the classes effects things. But if I had to guess, that's you're problem. Because my code works fine with no classes (just an assignment for one of my classes I had to do):
Focus on Problem 2 cause is has the creation of the new TopLevel every time the cursor is over a shape in the canvas. If I didn't implement the poptest function then for every main loop iteration in which the cursor was over a shape in the Problem 2 project a new TopLevel would be created (Literally hundreds or thousands if I remember correctly). And I can't attach the data sheet I use to test Problem 1 lol. Just a text file formatted like this with whatever data works:
These might be useful; resources if you're not using them already:
Sorry I couldn't give you a definitive answer, I wish you all the luck.
Glad I could help!