【问题标题】:Python tkinter opening one window instead of twoPython tkinter 打开一个窗口而不是两个
【发布时间】:2018-04-03 05:12:55
【问题描述】:

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

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

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

    你真的不应该有两个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()
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-17
        • 2013-09-18
        • 2018-05-11
        • 1970-01-01
        • 2017-10-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多