簡體   English   中英

python tkinter點擊按鈕打開新窗口

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

我在我的窗口上做了 3 個按鈕。 我選擇主窗口應該有一個特定的背景圖像和全屏。

現在有一個問題。 我想通過單擊按鈕 3 移至新窗口(頁面)(具有其他背景和其他內容)。

我嘗試過的事情:

  1. 從 Main.Info.travelhistory 導入 *

    • 我已將此添加到主窗口以打開一個新的 python 文件,其中包含單擊按鈕 3 時必須打開的第二個屏幕的代碼。但我發現如果我這樣做,兩個窗口將在運行主窗口時打開。
  2. 我在開頭添加了 root1 = Tk(),在末尾添加了 root1.mainloop(),在它們之間添加了另一個窗口的代碼。 但這也行不通,它像上面一樣打開了 2 個窗口。

這些都是我的嘗試,我想不出更好的方法。 我可以,但背景會保持不變。 但是我必須將新窗口的背景更改為我制作的背景圖像...

知道我做錯了什么嗎?

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

您不應調用多個Tk()窗口。

相反,tkinter 有另一個名為Toplevel小部件,可用於生成新窗口。

請參閱下面的示例:

from tkinter import *

root = Tk()

def command():
    Toplevel(root)

button = Button(root, text="New Window", command=command)
button.pack()

root.mainloop()

這將打開您可以編輯的新窗口。

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