【问题标题】:How to make tkinter dialog box appear first?如何让 tkinter 对话框首先出现?
【发布时间】:2013-10-30 07:46:01
【问题描述】:

我对 python 很陌生。

我的编码如下。 当用户单击“单击按钮设置 CPE”时。按钮,将出现对话窗口并显示客户列表。

我的问题是当用户单击“单击按钮设置 CPE”时。按钮,Listing() 功能首先工作。那时主窗口已经死了。完成Listing()后,出现对话框。

如何使对话框先出现并在对话框出现后显示信息。

import Tkinter

class myWindow:
    addr = ''
    def __init__(self):

        self.mw = Tkinter.Tk()
        self.mw.option_add("*font", ("Arial", 15, "normal"))
        self.mw.geometry("+250+200")
        self.mw.title("Example of Custom Dialog-Window")

        # CPE
        self.btn_cpe = Tkinter.Button(self.mw, text = "Click button to setup CPE.", command = self.btnCPE)
        self.btn_cpe.pack(padx = 20, pady = 20)
        self.mw.mainloop()


    def btnCPE(self):

        self.dialogwindow = Tkinter.Toplevel()
        self.dialogwindow.title("Dialog Window")
        self.dialogwindow.geometry("+500+350")
        self.dialogwindow.maxsize(500, 350)
        self.dialogwindow.minsize(500, 350)

        self.lab1 = Tkinter.Label(self.dialogwindow, text = "Checking info")
        self.lab1.pack()

        self.lab_addr = Tkinter.Label(self.dialogwindow, text = "Address : ")
        self.lab_addr.pack()

        # Refresh
        self.btn_refresh = Tkinter.Button(self.dialogwindow, text = "Refresh", command = self.refresh)
        self.btn_refresh.pack()

        self.btn_cpe_exit = Tkinter.Button(self.dialogwindow, text = "Exit", command = self.dialogwindow.destroy )
        self.btn_cpe_exit.pack()

        # This is the important line: It tells the main-window to lock:
        self.dialogwindow.grab_set() 

        self.Listing()
        self.lab_addr['text'] = "address : " + self.addr;

    def Listing(self):
        # access the db and set the address into self.addr

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    试试这个:

    import Tkinter
    
    class myWindow:
        addr = ''
        def __init__(self):
            # CPE
            self.btn_cpe = Tkinter.Button(mw, text = "Click button to setup CPE.", command = self.btnCPE)
            self.btn_cpe.pack(padx = 20, pady = 20)
    
        def btnCPE(self):
    
            self.dialogwindow = Tkinter.Toplevel()
            self.dialogwindow.title("Dialog Window")
            self.dialogwindow.geometry("+500+350")
            self.dialogwindow.maxsize(500, 350)
            self.dialogwindow.minsize(500, 350)
    
            self.lab1 = Tkinter.Label(self.dialogwindow, text = "Checking info")
            self.lab1.pack()
    
            self.lab_addr = Tkinter.Label(self.dialogwindow, text = "Address : ")
            self.lab_addr.pack()
    
            # Refresh
            self.btn_refresh = Tkinter.Button(self.dialogwindow, text = "Refresh", command = self.refresh)
            self.btn_refresh.pack()
    
            self.btn_cpe_exit = Tkinter.Button(self.dialogwindow, text = "Exit", command = self.dialogwindow.destroy )
            self.btn_cpe_exit.pack()
    
            # This is the important line: It tells the main-window to lock:
            self.dialogwindow.grab_set() 
    
            self.Listing()
            self.lab_addr['text'] = "address : " + self.addr;
    
        def Listing(self):
            print "hola"
            # access the db and set the address into self.addr
    
    mw = Tkinter.Tk()
    app=myWindow()
    mw.option_add("*font", ("Arial", 15, "normal"))
    mw.geometry("+250+200")
    mw.title("Example of Custom Dialog-Window")
    mw.mainloop()
    

    & 另外,尽量不要在构造函数中包含 UI 部分。告诉我是否有帮助

    【讨论】:

    • 没有朋友。请在 Listing() 函数中添加 time.sleep(5) 并测试它。
    • 兄弟,我已经给你了它的要点......你想要运行 Listing() 的方式是你自己想办法。你可以使用睡眠,甚至可以使用按钮来调用它。这里,对话框将首先出现,其余的由你决定你甚至可以在窗口被销毁后调用 Listing()跨度>
    猜你喜欢
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多