Python 中的线程和时间 + Tkinter [英] Threads And Time + Tkinter In Python

查看:42
本文介绍了Python 中的线程和时间 + Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 tkinter 中,我制作了一个密码 GUI,有些人帮助我解决了其他问题.问题是:我制作了一个名为 timeload 的文件,其中有一个 while 循环.当我将它导入我的程序时,它会卡在 import 上,因为 timeload 内部存在循环.我认为有一种方法可以用线程来做到这一点,但我不知道如何在我的代码中实现它.主要代码如下:

In tkinter I have made a password GUI some people have helped me with other things with it. The problem is: I have made a file called timeloadin it there is a while loop. When I import it into my program it gets stuck on the importbecause inside oftimeloadthere is the loop. I think there is a way to do it with threads but I do not know how to implement that into my code. Here is the main code:

import tkinter as tk
from threading import Thread
import timeload


class FirstFrame(tk.Frame):
    trys = 3
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.pack()
        master.title("Enter password")
        master.geometry("300x300")
        self.clock = tk.Label(self, fg='blue')
        self.clock.config(text=self.timeload.timeset())
        self.clock.pack()
        self.status2 = tk.Label(self, fg='blue')
        self.status2.pack()
        self.status = tk.Label(self, fg='red')
        self.status.pack()
        self.number = tk.Label(self, fg='red')
        self.number.pack()
        self.trysremain = tk.Label(self, fg='red')
        self.trysremain.pack()
        self.userlbl = tk.Label(self, text='Enter Username')
        self.userlbl.pack()
        self.userE = tk.Entry(self)
        self.userE.pack()
        self.userE.focus()
        self.lbl = tk.Label(self, text='Enter Password')
        self.lbl.pack()
        self.pwd = tk.Entry(self, show="*") 
        self.pwd.pack()
        self.pwd.bind('<Return>', self.check)
        self.btn = tk.Button(self, text="Done", command=self.check)
        self.btn.pack()
        self.btn = tk.Button(self, text="Cancel", command=self.quit)
        self.btn.pack()

    def check(self, event=None):

        if self.pwd.get() == app.password:
            if self.userE.get() == app.user:
                 self.destroy()
                 self.app= SecondFrame(self.master)
            else:
                self.status2.config(text="Wrong Username")

        else:
            self.trys = self.trys - 1
            self.status.config(text="Wrong password")
            self.number.config(text=self.trys)
            self.trysremain.config(text="Trys remaining")
           if self.trys == 0:
                root.destroy()
                root.quit()


class SecondFrame(tk.Frame):
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.pack()
        master.title("Main Application")
        master.geometry("600x400")
        self.c = tk.Button(self, text="Options", command=self.third_frame_open)
        self.c.pack()

    def third_frame_open(self):
        self.destroy()
        self.app= ThirdFrame(self.master)


class ThirdFrame(tk.Frame):
    def __init__(self, master, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)
        self.pack()
        self.password_set = tk.Label(self, fg='green')
        self.password_set.pack()
        master.title("Options")
        master.geometry("400x300")
        self.but2 = tk.Button(self, text="Go Back",     command=self.second_frame_open)
        self.but2.pack()
        self.but1 = tk.Button(self, text="Change password", command=self.showpasswordinput)
        self.but1.pack()
        self.but1.bind('<Return>', self.showpasswordinput)

    def showpasswordinput(self):
        self.but1.pack_forget()
        self.e = tk.Entry(self.master, show="*")
        self.e.pack()
        self.e.focus()
        self.but2 = tk.Button(self, text="Change password", command=self.set_password)
       self.but2.pack()
        self.but2.bind('<Return>', self.set_password)

    def set_password(self):
        self.password_set.config(text="Password Updated")
        setpass = open("password_store.txt", "w")
        passvar = self.e.get()
        self.e.pack_forget()
        setpass.write(passvar)
        setpass.close()

    def second_frame_open(self):
        self.destroy()
        self.app= SecondFrame(self.master)

if __name__=="__main__":
    root = tk.Tk()
    app=FirstFrame(root)
    user = open("user_store.txt", "r")
    app.user = user.read()
    user.close()
    password2 = open("password_store.txt", "r")
    app.password = password2.read()
    password2.close()
    root.mainloop()

这是timeload中的代码:

import datetime
timeload = ('on')
while timeload == ('on'):
    timeset = datetime.datetime.now()

谢谢,杰克

推荐答案

您不应该在 tkinter 中使用长循环或无限循环,它们会阻止 GUI 响应用户操作.定期更新时间等字段的正确方法是使用 tkinter .after 方法.

You shouldn't use long or infinite loops in tkinter, they will prevent the GUI from responding to user actions. A correct way to periodically update a field such as time is to use the tkinter .after method.

请参阅下面的基本程序示例,其中每 1 秒用当前时间更新标签.

See the below example of a basic program where a label is updated with the current time every 1 second.

try:
    import tkinter as tk
except:
    import Tkinter as tk

import datetime

class App(tk.Frame):
    def __init__(self,master=None,**kw):
        #Create the widgets
        tk.Frame.__init__(self,master=master,**kw)
        self.timeStr = tk.StringVar()
        self.lblTime = tk.Label(self,textvariable=self.timeStr)
        self.lblTime.grid()
        #Call the update function/method to update with current time.
        self.update()

    def update(self):
        self.timeStr.set(datetime.datetime.now())
        ## Now use the .after method to call this function again in 1sec.
        self.after(1000,self.update)


if __name__ == '__main__':
    root = tk.Tk()
    App(root).grid()
    root.mainloop()

这篇关于Python 中的线程和时间 + Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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