【问题标题】:How can I open a new window when the user clicks the button?用户单击按钮时如何打开新窗口?
【发布时间】:2015-03-01 17:05:44
【问题描述】:

当用户单击按钮时,我将如何创建一个新窗口(仍需要创建)?我已经取出了一些代码来缩短它。我需要创建一个按钮,当他们点击该按钮时,会打开一个新窗口。我没有创建按钮,因为按钮必须链接到新窗口。请帮忙

My imports...

class App:

    def __init__(self, master):
        self.master = master

        # call start to initialize to create the UI elemets
        self.start()

    def start(self):
        self.master.title("E-mail Extranalyser")
        self.now = datetime.datetime.now()

        tkinter.Label(
            self.master, text=label01).grid(row=0, column=0, sticky=tkinter.W)


        # CREATE A TEXTBOX
        self.filelocation = tkinter.Entry(self.master)
        self.filelocation["width"] = 60
        self.filelocation.focus_set()
        self.filelocation.grid(row=0, column=1)


        # CREATE A BUTTON WITH "ASK TO OPEN A FILE"
        # see: def browse_file(self)
        self.open_file = tkinter.Button(
            self.master, text="Browse...", command=self.browse_file)
        # put it beside the filelocation textbox
        self.open_file.grid(row=0, column=2)

        # now for a button
        self.submit = tkinter.Button(
            self.master, text="Execute!", command=self.start_processing,
            fg="red")
        self.submit.grid(row=13, column=1, sticky=tkinter.W)

    def start_processing(self):
        #code here


    def browse_file(self):
        # put the result in self.filename
        self.filename = filedialog.askopenfilename(title="Open a file...")

        # this will set the text of the self.filelocation
        self.filelocation.insert(0, self.filename)

root = tkinter.Tk()
app = App(root)
root.mainloop()

【问题讨论】:

  • 我有,但没有多大帮助
  • 其实链接的问题回答得很好。只需创建一个Toplevel 窗口。 “我没有创建按钮,因为按钮必须链接到新窗口”是什么意思?

标签: python tkinter


【解决方案1】:

使用Toplevel 打开一个新的。如下所示修改您的代码。

self.NewWindow = tkinter.Button(self.master, 
                                text="New Window", 
                                command=self.CreateNewWindow)

 def CreateNewWindow(self):
     self.top = tkinter.Toplevel()
     self.top.title("title")

【讨论】:

    【解决方案2】:

    看看https://www.youtube.com/watch?v=jBUpjijYtCk。完成本教程可能会对您有所帮助,但此特定视频展示了如何处理多个页面。

    类似这样的:

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

    【讨论】:

      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2011-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多