简体   繁体   English

python tkinter点击按钮打开新窗口

[英]python tkinter clicking on button to open new window

I've made 3 buttons on my window.我在我的窗口上做了 3 个按钮。 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.我想通过单击按钮 3 移至新窗口(页面)(具有其他背景和其他内容)。

Things i tryd:我尝试过的事情:

  1. from Main.Info.travelhistry import *从 Main.Info.travelhistory 导入 *

    • 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.我已将此添加到主窗口以打开一个新的 python 文件,其中包含单击按钮 3 时必须打开的第二个屏幕的代码。但我发现如果我这样做,两个窗口将在运行主窗口时打开。
  2. I added root1 = Tk() at the beginning, root1.mainloop() at the end and between them the code for the other window.我在开头添加了 root1 = Tk(),在末尾添加了 root1.mainloop(),在它们之间添加了另一个窗口的代码。 But this won't work also, its opening 2 windows like above.但这也行不通,它像上面一样打开了 2 个窗口。

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.您不应调用多个Tk()窗口。

Instead, tkinter has another widget called Toplevel which can be used to generate a new window.相反,tkinter 有另一个名为Toplevel小部件,可用于生成新窗口。

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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