I was testing the Toplevel() feature of python Tkinter and I was wondering if a user closes one window how do I keep the other one from closing as well?
from Tkinter import *
root = Tk()
top = Toplevel()
root.mainloop()
Calling transient, closing toplevel will not close the all windows.
from Tkinter import *
root = Tk()
top = Toplevel()
top.title('TopLevel window')
top.transient(root) # <------
root.mainloop()
Related
I will try to simplify my question here. My problem is like, on my first window of tkinter I have a button that opens another tkinter window. You can say it a second window to keep it on top I use win2.attributes('-topmost', True). Then I have a browse button to import file from the computer but when I click it goes under the window 2.
Following is my code.
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse():
file_to_open = askopenfilename()
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
Button2 = Button(win2, text="Browse", command=browse).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()
My browse window is going under the window 2. Can you please suggest me the best way to keep it on top. I am sharing the screenshot of the problem as well
Starting code
Click on new window
Click browse button and the browse window is hidden
Cheers
You need to set the parent option of askopenfilename() to the toplevel window win2:
from tkinter import *
from tkinter.filedialog import askopenfilename
root = Tk()
root.title("Base Window")
root.geometry("300x300")
def browse(parent):
file_to_open = askopenfilename(parent=parent)
def new_window():
win2 = Toplevel(root)
win2.title("Window 2")
win2.geometry("300x300")
win2.attributes('-topmost', True)
# pass 'win2' to browse()
Button2 = Button(win2, text="Browse", command=lambda:browse(win2)).pack()
button1 = Button(root, text="New window", command=new_window).pack()
root.mainloop()
I tried win.overrideredirect(True) it didn't work...
My Code goes like this:
from tkinter import *
win = Tk()
win.resizable(0,0)
win.wm_protocol("WM_SAVE_YOURSELF", lambda: print("On exit"))
Label(win, text="Tk Window").pack()
win.mainloop()
Specs:
Python 3.9.6 [Latest],
Pip 21.1.3,
OS: Windows 10 Home
I want to make the minimize button to be disabled...
Please help
The thing #N1CK145 posted didn't work for me but I am guessing that he/she tried to do this:
import Tkinter as tk
root = tk.Tk()
root.attributes("-toolwindow", True)
root.mainloop()
If you want to know more attribute options look here
try resizeable function like this :
win= Tk()
win.geometry("750x250")
win.resizable(False, False)
Found this here:
import Tkinter as tk
root= tk.Tk()
root.title("wm min/max")
# this removes the maximize button
root.resizable(0,0)
# # if on MS Windows, this might do the trick,
# # but I wouldn't know:
# root.attributes(toolwindow=1)
# # for no window manager decorations at all:
# root.overrideredirect(1)
# # useful for something like a splash screen
root.mainloop()
I'm trying to do something when a window is manually closed in tkinter and don't know a method/function to do so. A simple example of what im trying to do in a smaller scale.
from tkinter import *
root = Tk()
rootIsClosed = False # how to do this? How to make it True?
if rootIsClosed: # ?
new_win = Tk()
x = Label(new_win, text='why did you close the program').pack()
You can look at this answer which talks about how to handle exiting a window.
In short, you can do this:
from tkinter import *
root = Tk() #create new root
def on_close():
#this is the code you provided
new_win = Tk()
x = Label(new_win, text='why did you close the program').pack()
root.protocol("WM_DELETE_WINDOW", on_close) # main part of the code, calls on_close
root.mainloop()
Note that this doesn't allow you to actually close the window, so you can do this to close the window:
from tkinter import *
root = Tk()
def on_close():
new_win = Tk()
x = Label(new_win, text='why did you close the program').pack()
root.destroy() #only difference, root.destroy closes the original window.
root.protocol("WM_DELETE_WINDOW", on_close)
root.mainloop()
root = Tk()
root.geometry("1600x800+0+0")
root.title("Tronios Exportzendingen")
invoerscherm = Toplevel()
invoerscherm.geometry("800x400+0+0")
invoerscherm.title("Nieuwe Zending Invoeren")
root.mainloop()
Both windows are opening when executing the code.
I want the toplevel to open with a button in the root window.
How can i do that?
You never made a button, you can create a button and set the command.
root = Tk()
root.geometry("1600x800+0+0")
root.title("Tronios Exportzendingen")
def set_button():
invoerscherm = Toplevel()
invoerscherm.geometry("800x400+0+0")
invoerscherm.title("Nieuwe Zending Invoeren")
but = Button(text="Press Me", command=set_button)
but.pack()
root.mainloop()
I'd like to be able to open the App GUI and have it automatically place the cursor into a particular text widget. Best case scenario is: as soon as the app is launched someone can start typing without having to click on the text widget. This is just a small example displaying the issue:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
root.mainloop()
You use the focus_set method. For example:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
TextWidget.focus_set()
root.mainloop()