当用户单击按钮时如何打开一个新窗口? [英] How can I open a new window when the user clicks the button?

查看:48
本文介绍了当用户单击按钮时如何打开一个新窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 that button, a new window opens. I haven't created the button because the button has to be linked to the new window. Please help

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 打开一个新的.修改您的代码,如下所示.

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

这篇关于当用户单击按钮时如何打开一个新窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆