how can I code for a new window? I have a button that creates a new one but I like to code for it and I don't know how. I think I must define the new window in any way but I don't know how to do this because for opening a new window with help of a button you must define the window self, but no name.
Thanks for helping!
I have create the button and its command in this way:
from Tkinter import *
import Tkinter as tk
master = tk.Tk()
def create_window(): #Definion und Festlegung neues Fenster
toplevel = Toplevel()
toplevel.title('result')
toplevel.geometry('1500x1000')
toplevel.focus_set()
Button(master, text='forward', command=create_window).pack(padx=5, anchor=N, pady=4)
master.mainloop()
Coding for the new window (or creating widgets in the new window) is similar to how you do it in the main window. Just pass the new window (toplevel) as the parent.
Here is an example that creates a Label and an Entry widgets in the new window.
from Tkinter import *
import Tkinter as tk
master = tk.Tk() # Create the main window
def create_window(): #Definion und Festlegung neues Fenster
toplevel = Toplevel()
toplevel.title('result')
toplevel.geometry('1500x1000')
# Create widges in the new window
label = tk.Label(toplevel, text="A Label", fg='blue')
entry = tk.Entry(toplevel)
label.pack()
entry.pack()
toplevel.focus_set()
Button(master, text='forward', command=create_window).pack(padx=5, anchor=N, pady=4)
master.mainloop()
Related
In Tkinter, (Python), I want to add a button that creates a new window with some other widgets such as other buttons and Labels. How do I do that?
If I create a button as:
Button(text = "click", command = new_window).pack()
So now what code should I use in the new_window() function in my program?
You can use Toplevel for that.
from tkinter import Tk, Toplevel
from tkinter.ttk import Label, Button
root = Tk()
root.title("Creating multiple windows")
root.geometry("500x500")
def new_window():
top = Toplevel()
top.title("Second window")
top.geometry("400x500") # By default, it is kept as the geometry of the main window, but you can change it.
lab = Label(top, text="This is second window!")
lab.pack(pady=20)
l = Label(root, text="This is the first window")
l.pack(pady=20)
b = Button(root, text="Create new window", command=new_window)
b.pack(pady=50)
root.mainloop()
You can use the “Toplevel” widget for that.
Here’s how you can do that:
from tkinter import *
# NOTE: You can replace the variable names with your own
# Defining a function for a new window to appear
def new_window():
# You can also customize this toplevel with similar attributes
new_toplevel = Toplevel()
# Creating the main GUI
root = Tk()
# You can customize the root window with attributes like .title(), .iconbitmap(), .geometry() etc.
new_button = Button(text=“New Window”, command=new_window)
new_button.pack(pady=10, padx=10) # Paddings are optional and you can also use .grid() with slightly different arguments
root.mainloop()
My Code opens a window with a button. When the button gets clicked a toplevel window is created and the root window gets destroyed. When the button on the toplevel window gets clicked a messagebox opens. I want the the Toplevel Window to be closed when the user presses the ok button of the messagebox.
Pressing Ok causes the TypeError: destroy() missing 1 required positional argument: 'self'
I really don't understand why it dosn't work since the toplevel window gets passed as an argument to the destroy method.
import tkinter as tk
from tkinter import messagebox
def main():
root = tk.Tk()
root.title("Hauptmenü")
mainmenue(root)
root.mainloop()
def mainmenue(root):
button_rennen = tk.Button(root, text="New Window", width=20,
command=lambda: call_window(root))
button_rennen.pack()
def call_window(root):
root.destroy()
rframe = tk.Toplevel
button = tk.Button(text="Wette platzieren",
command=lambda: question(rframe))
button.pack()
def question(rframe):
dialog = tk.messagebox.askokcancel(message="Destroy Window?")
if dialog is True:
rframe.destroy()
main()
def call_window(root):
root.destroy()
rframe = tk.Tk()
button = tk.Button(rframe, text="Wette platzieren",
command=lambda: question(rframe))
button.pack()
replace toplevel with Tk window and it works fine.
I am working on code in tkinter python v3.7 where I want to open a new window which has same functionalities like original window. How can I do that?
While searching for solution I came across function naming Toplevel which creates new tkinter window. But this new window is completely new, It doesn't have functionalities(Button, geometry size in my case) which were provided in original one.
from tkinter import *
class TextPad:
def new_window(self):
top = Toplevel()
def __init__(self, master):
master.title('Text Pad')
master.geometry('400x400')
self.button = Button(master, text='Press',
command=self.new_window)
self.button.pack()
root = Tk()
t = TextPad(root)
root.mainloop()
My original window has geometry size of '400x400' and It has 'button', I want to open a new window having this functionalities.
I assume you want two (or more) windows at the same time.
If you want identical window then use again TextPad()
but this time use Toplevel() instead of Tk()
def new_window(self):
top = TextPad(Toplevel())
and don't run second mainloop()
If you want replace first window by new window - to have alwasy only one window - then you would have to destroy old window and then create new one using TextPad() with Tk() and mainloop()
But it would need to use self.master instead of master to have access to master in method new_window
from tkinter import *
class TextPad:
def new_window(self):
self.master.destroy()
root = Tk()
t = TextPad(root)
root.mainloop()
def __init__(self, master):
self.master = master
self.master.title('Text Pad')
self.master.geometry('400x400')
self.button = Button(self.master, text='Press',
command=self.new_window)
self.button.pack()
root = Tk()
t = TextPad(root)
root.mainloop()
When I make a button that closes the current window and opens another, the current window doesn't close.
from tkinter import *
root = Tk()
def new_window():
root.quit()
new_window = Tk()
new_window.mainloop()
Button(root, text="Create new window", command=new_window).pack()
root.mainloop()
(This isn't my program, it's just an example)
You should be able to do it like this:
import tkinter as tk
root = tk.Tk()
def new_window():
root = tk.Tk()
test = tk.Button(root, text="Create new window", command= lambda:[root.destroy(), new_window()]).pack()
root.mainloop()
test = tk.Button(root, text="Create new window", command= lambda:[root.destroy(), new_window()]).pack()
root.mainloop()
This will literally keep opening the exact same window with a button. The lambda allows you to call multiple functions. By calling .destroy() on your root window, it destroys your window, but doesn’t stop the program. Then you create a new root window with your function.
You can use this technique on your actual script.
I've made 3 buttons on my window. I choosed that the main window should have a specific background image and a full screen.
Now there is a problem. I would like to move to a new window (page) (with an other background and other things) by clicking on button 3.
Things i tryd:
from Main.Info.travelhistry import *
I've added this to the main window to open a new python file with the code of the second screen that has to open when clicking on button 3. But I found out that if I do this both windows will open when running main window.
I added root1 = Tk() at the beginning, root1.mainloop() at the end and between them the code for the other window. But this won't work also, its opening 2 windows like above.
Those were all my attempts and i cant figure out a better way. I can but the background would stay the same. But I have to change the background for the new window to a background image i made...
Any idea what im doing wrong?
from tkinter import *
from tkinter.messagebox import showinfo
from Main.Info.travelhistry import *
def clicked1():
bericht = 'Deze functie is uitgeschakeld.'
showinfo(title='popup', message=bericht)
root = Tk()
a = root.wm_attributes('-fullscreen', 1)
#Hoofdmenu achtergrond
C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file = "test1.png")
background_label = Label(root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()
# Geen OV-chipkaart button
b=Button(master=root, command=clicked1)
photo=PhotoImage(file="button1.png")
b.config(image=photo,width="136",height="53", background='black')
b.place(x=310, y=340)
#Buitenland button
b2=Button(master=root, command=clicked1)
photo1=PhotoImage(file="button2.png")
b2.config(image=photo1,width="136",height="53", background='black')
b2.place(x=490, y=340)
#Reis informatie
b3=Button(master=root)
photo2=PhotoImage(file="button3.png")
b3.config(image=photo2,width="136",height="53", background='black')
b3.place(x=680, y=340)
root.mainloop()
root2.mainloop()
You shouldn't call more than one Tk() window.
Instead, tkinter has another widget called Toplevel which can be used to generate a new window.
See below for an example:
from tkinter import *
root = Tk()
def command():
Toplevel(root)
button = Button(root, text="New Window", command=command)
button.pack()
root.mainloop()
This one opens new window that you can edit.
from tkinter import *
Window = Tk()
def Open():
New_Window = Tk()
#You can edit here.
New_Window.mainloop()
Btn1 = Button(text="Open", command=Open)
Bt1n.pack()
Window.mainloop()