简体   繁体   中英

Threads And Time + Tkinter In Python

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 timeload in it there is a while loop. When I import it into my program it gets stuck on the import because inside of timeload there 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()

Here is the code in the timeload :

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

Thanks, Jake

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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