简体   繁体   中英

python tkinter clicking on button to open new window

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:

  1. 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.
  2. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM