How can I open a new window when the user clicks the button?

后端 未结 2 510
感动是毒
感动是毒 2021-01-26 02:12

How would I create a new window when the user clicks a button (still needs creating)? I have took some code out to make this shorter. I need a button creating and when they hit

相关标签:
2条回答
  • 2021-01-26 02:37

    Take a look at https://www.youtube.com/watch?v=jBUpjijYtCk. Working through this tutorial would probably help you but this specific video shows how to work with multiple pages.

    Something like this:

        from tkinter import *
    
        class Sample(Tk):
            def __init__(self,*args, **kwargs):
                Tk.__init__(self, *args, **kwargs)
                container = Frame(self)
                container.pack(side="top", fill="both", expand = True)
    
                self.frames = {}
    
                for F in (MainPage, OtherPage):
                    frame=F(container, self)
                    self.frames[F]=frame
                    frame.grid(row=0, column=0, sticky="nsew")
    
                self.show_frame(MainPage)
    
            def show_frame(self, page):
                frame = self.frames[page]
                frame.tkraise()
    
        class MainPage(Frame):
            def __init__(self, parent, controller):
                Frame.__init__(self, parent)
                Label(self, text="Start Page").pack()
                Button(self, text="other page?", command=lambda:controller.show_frame(OtherPage)).pack()
    
        class OtherPage(Frame):
            def __init__(self, parent, controller):
                Frame.__init__(self, parent)
                Label(self, text="Next Page").pack()
                Button(self, text="back", command=lambda:controller.show_frame(MainPage)).pack()
    
        app = Sample()
        app.mainloop()
    
    0 讨论(0)
  • 2021-01-26 02:41

    Use a Toplevel to open a new one. Modify your code as shown below.

    self.NewWindow = tkinter.Button(self.master, 
                                    text="New Window", 
                                    command=self.CreateNewWindow)
    
     def CreateNewWindow(self):
         self.top = tkinter.Toplevel()
         self.top.title("title")
    
    0 讨论(0)
提交回复
热议问题