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

时间:2017-10-22 15:51:21

标签: python user-interface tkinter

我正在使用tkinter开发GUI。 我有一个主菜单,在这个菜单里面有三个按钮。我想通过点击按钮3打开一个新窗口。在我的代码中,它现在正在做我几乎想要的事情。但是你可以看到我已经在按钮3中添加了一个命令来销毁主根以转到第二个。 但这会导致问题,例如:当我想关闭主菜单时,它将自动打开第二个根。我只是想要有创意,因为我无法找到另一种方法来打开一个具有不同背景图像的新窗口。 我可以用来使生活更轻松的任何想法,技巧或功能? 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()

2 个答案:

答案 0 :(得分:0)

你真的不应该有两个Tk个实例。 tkinter并非旨在以这种方式工作。如果您想在根目录之上添加另一个窗口,请使用Toplevel。另外,我建议你坚持使用OOP方法,并将你的窗口保持为单独的类。例如:

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

答案 1 :(得分:0)

这将通过使用Toplevel而不是调用两个Tk实例来解决您的问题。如果这样做,您可以添加全屏属性和图像。 Toplevel表示根窗口的从属窗口,因此如果关闭根窗口,它将关闭Toplevel窗口。我将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()