简体   繁体   English

Python Tkinter打开一个窗口而不是两个

[英]Python tkinter opening one window instead of two

I'm working on a GUI using tkinter. 我正在使用tkinter在GUI上工作。 I got a main menu and inside this menu three buttons. 我有一个主菜单,在此菜单内有三个按钮。 I would like to open a new window by clicking on button 3. In my code now its doing what i almost want. 我想通过单击按钮3打开一个新窗口。在我的代码中,它现在正在执行我几乎想要的操作。 BUT as you can see i've added to button 3 a command to destroy the main root to go to the second one. 但是,正如您所看到的,我已经在按钮3中添加了一个销毁主根的命令以转到第二根。 But this will cause problems for example: When I want to close the main menu it will open the second root automaticly. 但这会引起问题,例如:当我想关闭主菜单时,它将自动打开第二个根目录。 I just tryd to be creative because i couldnt find another way out to open a new window with a different background image. 我只是想发挥创造力,因为我找不到其他方法来打开一个具有不同背景图像的新窗口。 Any ideas, trics or functions that i could use to make life easier? 我可以用来简化生活的任何想法,度量标准或功能吗? mycode: mycode:

from tkinter import *
from tkinter.messagebox import showinfo


def clicked1():
    bericht = 'Deze functie is uitgeschakeld.'
    showinfo(title='popup', message=bericht)

root = Tk()

def quit():
    root.destroy()

a = root.wm_attributes('-fullscreen', 1)
#full screen


#w, h = root.winfo_screenwidth(), root.winfo_screenheight()
#root.geometry("%dx%d+0+0" % (w, h))


#Hoofdmenu achtergrond
C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file="C:\\Users\\Downloads\\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="C:\\Users\\Downloads\\button1.png")
b.config(image=photo,width="136",height="53", background='black')
b.place(x=310, y=340)

#exit button
exitbut = PhotoImage(file = "C:\\Users\\Downloads\\exit1.png")
starter = Label(image = exitbut)
starter.pack()

start = Label(image = exitbut)
start.place(x=900, y=140)

#Buitenland button
b2=Button(master=root, command=clicked1)
photo1=PhotoImage(file="C:\\Users\\Downloads\\button2.png")
b2.config(image=photo1,width="136",height="53", background='black')
b2.place(x=490, y=340)

#Reis informatie
b3=Button(master=root, command=quit)
photo2=PhotoImage(file="C:\\Users\\Downloads\\button3.png")
b3.config(image=photo2,width="136",height="53", background='black')
b3.place(x=680, y=340)


root.mainloop()


#2e window-------------------------------------------------------------
root2 = Tk()

#full screen
a = root2.wm_attributes('-fullscreen', 1)

#achtergrond
D = Canvas(root2, bg="blue", height=250, width=300)
filename = PhotoImage(file = "C:\\Users\\Downloads\\leeg.png")
background_label = Label(root2, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
D.pack()

# Geen OV-chipkaart button
c1=Button(master=root2, command=clicked1)
photo3=PhotoImage(file="C:\\Users\\Downloads\\mijnlocatie.png")
c1.config(image=photo3,width="136",height="53", background='black')
c1.place(x=210, y=70)

# Geen OV-chipkaart button
c2=Button(master=root2, command=clicked1)
photo4=PhotoImage(file="C:\\Users\\Downloads\\overigelocaties.png")
c2.config(image=photo4,width="136",height="53", background='black')
c2.place(x=210, y=140)


root2.mainloop()

You really shouldn't have two Tk instances. 您真的不应该有两个Tk实例。 tkinter is not designed to work that way. tkinter并非以这种方式工作。 If you want another window on top of your root one, use Toplevel . 如果您希望在根目录之上有另一个窗口,请使用Toplevel Also, I suggest you stick to OOP approach and keep your windows as separate classes. 另外,我建议您坚持使用OOP方法并将Windows保持为单独的类。 For example: 例如:

import tkinter as tk

class App(tk.Tk):

    def __init__(self):
        super().__init__()
        self.create_widgets()

    def create_widgets(self):
        """
        Instantiating all root window widgets
        """
        tk.Button(self, text='Open another window', command=self.open_dialog).pack()

    def open_dialog(self):
        d = Dialog(self)
        d.wait_window()

class Dialog(tk.Toplevel):

    def __init__(self, parent):
        super().__init__(parent)
        self.create_widgets()

    def create_widgets(self):
        """
        Instantiating all toplevel window widgets
        """
        tk.Label(self, text='Welcome to another window!').pack(padx=20, pady=50)

if __name__ == '__main__':
    app = App()
    app.mainloop()

This will solve the problem for you by using Toplevel instead of calling two Tk instances . 这将通过使用Toplevel而不是调用两个Tk实例为您解决问题。 You can add your full screen attribute and images if you do it this way. 如果您这样做,则可以添加全屏属性和图像。 Toplevel means slave window to the root window so if you close the root window it will close the the Toplevel window to . Toplevel表示从属窗口是根窗口,因此,如果您关闭根窗口,它将关闭Toplevel窗口。 I changed your quit function to quit_window as you can also root.quit() to close the window. 我将您的quit函数更改为quit_window因为您也可以root.quit()关闭窗口。

from tkinter import *


def slave1():
    tp = Toplevel()
    tp.geometry("400x400")
    b = Button(tp, text="button") # you can add you image to it using photoimage
    b.place(x=200, y=200)


def quit_root():
    root.destroy()



root = Tk()
root.geometry("500x500")

button1 = Button(root, text="button one", command=slave1)
button2 = Button(root, text="button two")
button3 = Button(root, text="button three", command=quit_root)

button1.place(x=210, y=340)
button2.place(x=340, y=370)
button3.place(x=370, y=420)

root.mainloop()

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

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