Tkinter启动窗口

时间:2020-05-19 16:06:38

标签: python tkinter

我正在尝试在tkinter中创建一个启动窗口。当程序通过其检查清单运行时,我要显示确认每个步骤均已成功,并且用户不认为程序已挂起以等待主屏幕。然后它必须关闭窗口并继续执行主程序。

一旦循环完成,我尝试过的所有操作都只会在窗口中显示结果。有没有一种方法可以显示结果,一次行,每步之间有1秒的延迟,然后再进入主程序?我可能选择了错误的方法吗?

到目前为止,我从较大的程序中摘录了以下内容

def check_presets_path():
    if os.path.isdir("Presets"):
        #Directory exists
        print("Here at Preset Paths")
        Label(boot_window, text = 'Preset Directory OK...').pack()#.grid()
        time.sleep(1)
    else:
        MsgBox = messagebox.showerror(title="Presets", message = 'Presets sub-directory does not exist')
        exit()

##############################################################################################################
def check_text_files_for_emails_path():
    if os.path.isdir("Text Files for Emails"):
        #Directory exists
        label = Label(boot_window, text = 'Text Files for Emails Directory OK...').pack()
        time.sleep(1)
    else:
        MsgBox = messagebox.showerror(title="Text Files for Emails sub-directory", message = 'Text Files for Emails sub-directory does not exist')
        exit()

###############################################################################################################

def check_log_files_path():
    if os.path.isdir("Log Files"):
        #Directory exists
        Label(boot_window, text = 'Log Files Directory OK...')#.grid()
        time.sleep(1)
    else:
        MsgBox = messagebox.showerror(title="Log Files sub-directory", message = 'Log Files sub-directory does not exist')
        exit()

###############################################################################################################

def check_database_path():
    if os.path.isdir("Database"):
        #Directory exists
        Label(boot_window, text = 'Database Directory OK...')#.grid()
        time.sleep(1)
    else:
        MsgBox = messagebox.showerror(title="Database Sub-Directory", message = 'Database sub-directory does not exist')
        exit()

##################################################################################################################################
#CHECK IF FILES EXIST AND ARE IN THE CORRECT DIRECTORY/SUB-DIRECTORY
def check_logo_ico():
    if os.path.isfile("Presets/logo.ico"):
        #Is valid
        Label(boot_window, text = 'Logo file OK...')#.grid()
        time.sleep(1)
    else:
        MsgBox = messagebox.showerror(title="Presets/logo.ico", message = 'logo.ico in the presets sub-directory does not exist')
        exit()

###################################################################################################################################
#CHECK IF FILES EXIST AND ARE IN THE CORRECT DIRECTORY/SUB-DIRECTORY

def check_database_db():
    if os.path.isfile("Database/address_book.db"):
        #Is valid
        Label(boot_window, text = 'Address Book Database OK...')#.grid()
        time.sleep(1)
    else:
        MsgBox = messagebox.showerror(title="Database/address_book.db", message = 'address_book.db in the Database sub-directory does not exist')
        exit()

#########################################################################################################################################

def check_button_logo_png():
    if os.path.isfile("Presets/button_logo.png"):
        #Is valid
        Label(boot_window, text = 'Button Logo OK...')#.grid()
        time.sleep(1)
    else:
        MsgBox = messagebox.showerror(title="Presets/button_logo.ico", message = 'button_logo.ico in the presets sub-directory does not exist')
        exit()


#########################################################################################################################################

def check_companydetails_text():
    try:
        f = open('Presets/CompanyDetails.txt')
    except FileNotFoundError:
        MsgBox = messagebox.showinfo(title="Company Details.txt", message = 'CompanyDetails.txt in the Presets sub-directory cannot be found')
        exit()
    else:
        Label(boot_window, text = 'Company Details File OK...')#.grid()
        time.sleep(1)
        f.close()   

#########################################################################################################################################

def check_emailsettings_txt():
    try:
        f = open('Presets/EmailSettings.txt')
    except FileNotFoundError:
        MsgBox = messagebox.showinfo(title="Email settings.txt", message = 'EmailSettings.txt in the Presets sub-directory cannot be found')
        exit()
    else:
        Label(boot_window, text = 'Email Settings File OK...').pack()
        time.sleep(1)
        f.close() 

#########################################################################################################################################

def check_runlog_txt():
    try:
        f = open('Log Files/RunLog.txt')
    except FileNotFoundError:
        MsgBox = messagebox.showinfo(title="Run Log.txt", message = 'RunLog.txt file cannot be found')
    else:
        Label(boot_window, text = 'Run Log File OK...').pack()
        time.sleep(1)
        f.close() 

########################################################################################################################################
# DISPLAY BOOT UP WINDOW ###############################################################################################################
########################################################################################################################################

def do_checking():
    check_text_files_for_emails_path()
    check_presets_path()
    check_log_files_path()
    check_database_path()
    check_logo_ico()
    check_database_db()
    check_button_logo_png()
    check_companydetails_text()
    check_emailsettings_txt()
    check_runlog_txt()


boot_window = Tk()
boot_window.title('BOOT WINDOW')
boot_window.geometry("250x350+700+200")

Label(boot_window, text = 'Checking Directory and Files').pack()

do_checking()
boot_window.after(10000, boot_window.destroy)

boot_window.mainloop()

main_program()

1 个答案:

答案 0 :(得分:0)

首先,我强烈建议您阅读PEP 8指南,并熟悉如何正确格式化Python代码。

我还建议您阅读有关threading.

的信息

您需要创建一个运行这些检查的新线程。否则,在运行boot_window之前,您将运行检查。您需要将boot_window作为参数发送给其他函数。

# at top...
import threading
# now down low...
boot_window = tk.Tk()
boot_window.title('BOOT WINDOW')
boot_window.geometry("250x350+700+200")
boot_window.after(10000, boot_window.destroy)
Label(boot_window, text='init text...').grid(row=1)
# New thread...
t = threading.Thread(target=do_checking, args=[boot_window])
t.start() # start your do_checking thread
boot_window.mainloop()

然后您的do_checking将具有boot_window的参数。

def do_checking(wind):
    check_presets_path(wind, 2)
    time.sleep(1)
    check_next_thing(wind, 3)
    time.sleep(1)
    # etc...

然后每个函数将获取分配的行并将标签放入您的wind

def check_presets_path(wind, row):
      if os.path.isdir('some_dir'):
          Label(wind, text = 'some_dir exists').grid(row=row)
      else:
          # message box stuff

这应该可以帮助您入门,但是如果您想成功地构建GUI,就必须对线程有更深入的了解