CLOSED new Tk window opens automatically - python-3.x

Hi I had this exact same question :
opening a new window with a button
But as I ran the code given : (EDIT : first line is indeed from tkinter import *)
from tkinter import *
def create_window():
window = Toplevel(root)
root = Tk()
b = Button(root, text="Create new window", command=create_window)
b.pack()
root.mainloop()
the new window opens automatically, without waiting for the button to be clicked
why is that?

Related

Create notepad via python

Scope - Python3.10
Looking for -
add multiple editable notes in tabs. (just like notepad)
How to add a save function for a specific note
enter image description here
Attaching work done till now, Help is much appreciated
from tkinter import *
from tkinter import ttk
MainWindow = Tk() #Object#
MainWindow.geometry ('380x250') #size of object#
MainWindow.title("Bnote") #Name of object#
Menubar = Menu(MainWindow)
Appearance = Menu(Menubar,tearoff=0)
NotesArea = Text(MainWindow)
def test(Menubar):
Menubar.entryconfigure(1, label="click")
file_menu = Menu(Menubar, tearoff=False)
file_menu.add_command(label="New_Tab", command=lambda: clicked(file_menu))
Menubar.add_cascade(label="+", menu=file_menu)
scrollbar = Scrollbar(MainWindow) #adding scrollbar#
scrollbar.pack(side=RIGHT,fill=Y) #packing scrollbar#
MainWindow.config(menu=Menubar)
NotesArea = Text(MainWindow,yscrollcommand=scrollbar.set,wrap = WORD, font="Ubuntu 12")
scrollbar.config(command=NotesArea.yview) # configuring scrollbar
NotesArea.pack(fill=BOTH)
MainWindow.mainloop ()

How do I display a message before closing the tkinter window?

A message is required to be shown just before closing the tkinter window on the click of a close button added to the window.
lab = Label(window,text = 'thank you')
lab.grid()
window.destroy()
I used the above code to do so, but the window gets closed before the message is being displayed
Can I have the solution for this?
You can either use this:
from tkinter import Tk
import tkinter.messagebox as msgbox
def display_msg():
msgbox.showinfo(title='', message='Thank You')
root.destroy()
root = Tk()
root.protocol('WM_DELETE_WINDOW', display_msg)
root.mainloop()
which will show You a messagebox before closing,
or if You want to display a widget use this:
from tkinter import Tk, Label
def display_msg():
msg = Label(root, text='Thank You!')
msg.pack()
root.after(3000, root.quit)
root = Tk()
root.protocol('WM_DELETE_WINDOW', display_msg)
root.mainloop()
When You protocol root with 'WM_DELETE_WINDOM' You can make it execute a function when You try to close the window.
In the first example it will just show an infobox.
In the second example it will wait for 3 seconds (3000 miliseconds) and then destroy the root

why does tkinter widgets doesnt appear in vscode?

from tkinter import *
from tkinter import ttk
root = Tk()
root.mainloop()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
And when i try to run the program only the window show up but without the button.
it gives me this error:
Exception has occurred: TclError
NULL main window
File "C:\Users\Elad\Desktop\coding\tkintertut.py", line 6, in <module>
button1 = ttk.Label(root, text = 'lol')
The root.mainloop() has to be at the end of the code for the window to be shown, not right after declaration of root.
from tkinter import *
from tkinter import ttk
root = Tk()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
root.mainloop()
Explanation:
Only the lines of code between root and root.mainloop() gets executed as long as the window is open, if you close the window, the Labels and all other widget declared after root.mainloop() become active but, the window is now closed and root is destroyed, hence the error. But here in my answer, the code between root and root.mainloop() has everything I need to be in the window and hence it shows the complete window. Just keep in mind to always say root.mainloop() at the end of the code only
Hope it cleared your errors. Do let me know if any more errors or doubts.
Cheers
Your root.mainloop() should be the last code!
from tkinter import *
from tkinter import ttk
root = Tk()
button1 = ttk.Label(root, text = 'lol')
button1.pack()
root.mainloop()

Tkinter Toplevel window not appearing

Python 3.8, Win 10 is the os, Toplevel widget does not appear to be working with new window not appearing. Any guidance would be appreciated, thanks!
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
top.mainloop()
my_button = Button(root, text="Popup, click here", command="popup")
my_button.grid(row=0, column=0)
root.mainloop()
Problem:
The only issue here is that the callback command shouldn't be a string.
Solution:
Remove the quotes around popup and the Toplevel window should appear.
Fixed Code:
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
my_label_top.pack()
my_button = Button(root, text="Popup, click here", command=popup)
my_button.grid(row=0, column=0)
root.mainloop()
Tips:
Using top.mainloop() is not necessary.
You also forgot to pack() the Label(my_label_top)

okay so I created this code to create a GUI but i need to add buttons and when ever i try it creates a new window. what should I do?

this is my code so far o cant add buttons with out it creating more windows
////////
#import tkinter
import tkinter
#import tkmessagebox(buttons)
from tkinter import *
#create a new window
window = tkinter.Tk()
#title <------ put it before .mainloop
window.title("yeahh boiiii")
#window size
window.geometry("500x500")
#set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico')#<---- 8bit file name
master = Tk()
def callback():
print ("click!")
b = Button(master, text="OK", command=callback)
b.pack()
#draws the window
window.mainloop()
////////
please help
Your problem is that you create 2 instances of Tk(). This is a bad idea, and you don't need to do it since you can make your button a child of the window object:
# Import tkinter
import tkinter as tk
# Create a new window
window = tk.Tk()
# Title <------ put it before .mainloop
window.title("yeahh boiiii")
# Window size
window.geometry("500x500")
# Set a window icon
window.iconbitmap('N:\downloads\icon.ico.ico') #<---- 8bit file name
def callback():
print ("click!")
b = tk.Button(window, text="OK", command=callback)
b.pack()
# Draw the window
window.mainloop()
I also rewrote your tkinter import, because you were importing it twice...

Resources