简体   繁体   English

如何使tkinter对话框首先出现?

[英]How to make tkinter dialog box appear first?

I'm very new to python. 我是python的新手。

I've coded as follows. 我编码如下。 When user clicks the "Click button to setup CPE." 当用户单击“单击按钮以设置CPE”时。 button, the dialog window will appear and display the customer list. 按钮,将出现对话框,并显示客户列表。

My problem is that when the user clicks the "Click button to setup CPE." 我的问题是,当用户单击“单击按钮以设置CPE”时。 button, the Listing() function is working first. 按钮, Listing()函数首先起作用。 At that time the main window is dead. 那时主窗口已死。 After finishing the Listing() , the dialog appears. 完成Listing() ,出现对话框。

What can I do to make the dialog box appear first and display the info after the dialog box appears. 我该怎么做才能使对话框首先出现,并在对话框出现后显示信息。

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

Try this: 尝试这个:

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

& also,try not to include the UI part in the constructor. 并且,尝试不要在构造函数中包含UI部分。 Tell me if it helps 告诉我是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM