-3
スレッドを使用してループを作る方法を尋ねる別の質問があります: Threads And Time + Tkinter In Python しかし、ループのようには機能しません。それはクロックをうまくロードしますが、更新されません。クロックがTkinterでdefを使用しようとしていません
import tkinter as tk
import datetime
class FirstFrame(tk.Frame):
trys = 3
timecall = datetime.datetime.now()
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.update()
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 update(self, event=None):
timecall = datetime.datetime.now()
self.clock.config(text=self.timecall)
self.after(1000, self.update)
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()
'self.clock.config(text = self.timecall)'は 'self.clock.config(text = timecall)'でなければなりません。 –
他のサイトのコードにリンクしないでください。 –
申し訳ありませんが、今私はそれを変更したことを知らなかった。うわー、私は4つのダウン投票に行きます。 – Jake