pythonで出退勤を行うシステムを構築中なのですが、TkinterでGUIを作成している最中にわからないことがありましたので質問させていただきます。
syukkin関数の中で新しく出勤というタイトルのウィンドウを作り、その中でsyukkin_act関数を呼び出して入力されたidmをmysqlのテーブルに書き込むという処理を行い、それをwhileで繰り返しているのですが、繰り返し処理をウィンドウを削除すると同時に終了させる方法がわかりません。
出勤ウィンドウに、前の画面に戻るというボタンを設置しているのですが、現在のコードだとそれをクリックして出勤ウィンドウを削除させることはできるのですが、繰り返し処理を終了させることが出来ない状態になっていると思います。
何か良い方法があれば教えていただきたいです。
よろしくお願いします。```python
コード
import tkinter as tk import sys import pymysql.cursors import nfc import binascii import string from datetime import datetime import threading connection = pymysql.connect( host='localhost', user='root', passwd='パスワード', db='データベース', charset = 'utf8', #cursorclass=pymysql.cursors.DictCursor ) cursor = connection.cursor() def timer(): time["text"] = "{}".format(datetime.now().strftime('%Y/%m/%d/ %H:%M:%S')) main.after(1, timer) def nf(): clf = nfc.ContactlessFrontend('usb') try: tag = clf.connect(rdwr={'on-connect': lambda tag: False}) finally: clf.close() idm = binascii.hexlify(tag.idm) return idm #記号削除 def kigo_rem(rem): kigo = string.punctuation table = str.maketrans( '', '', kigo) kigo2 = rem.translate(table) result = kigo2.replace("name ", "") return result def table_reset(add_name): for one_day in range(1, 32): with connection.cursor() as cursor: #add_nameテーブルのdayに1~31の数字を入れる sql = "INSERT INTO " + add_name + " (day) VALUES(%s)" cursor.execute(sql, (one_day)) connection.commit() def syuttai(table_name, syut): dt_now = datetime.now() year = str(dt_now.year) month = str(dt_now.month) day = str(dt_now.day) time = str(dt_now.hour) + ":" + str(dt_now.minute) with connection.cursor() as cursor: #メンバーの名前(row_str)のテーブルのyear,month,syukkinに現在時刻を挿入(UPDATE) sql = "UPDATE " + table_name + " SET year = %s, month = %s, " + syut + " =%s WHERE day = %s" cursor.execute(sql, (year, month, time, day)) connection.commit() def syukkin(): #最初のGUIの画面に戻る def return_main(): global loop loop = 0 syukkin_window.destroy() def syukkin_act(): loop = 1 while loop == 1: try: idm = nf() with connection.cursor() as cursor: #Memberslistテーブルからidmに対応したメンバーを探す sql = "select name from memberslist where idm = %s" cursor.execute(sql, (idm)) dbdata = cursor.fetchall() if dbdata == (): dbdata = '0' for row in dbdata: if row != '0': rem = str(row) row_str = kigo_rem(rem) dt_now = datetime.now() dt_in = dt_now.strftime('%Y年%m月%d日 %H:%M:%S') print(dt_in) lbl_syukkin1["text"] = dt_in print(f'{row_str}さんおはようございます') lbl_syukkin2["text"] = f'{row_str}さんおはようございます' day = str(dt_now.day) + "日" syut = "syukkin" if day == "1": with connection.cursor() as cursor: sql = "DELETE FROM " + row_str cursor.execute(sql) connection.commit() table_reset(row_str) syuttai(row_str, syut) else: syuttai(row_str, syut) else: #print('登録されていません') lbl_syukkin1["text"] = '登録されていません' lbl_syukkin2["text"] = '' finally: connection.commit() syukkin_window = tk.Tk() syukkin_window.geometry('400x300') syukkin_window.title('出勤') nf_lbl = tk.Label(syukkin_window, text = 'ICカードをタッチしてください。') nf_lbl.pack() lbl_syukkin1 = tk.Label(syukkin_window, text = '') lbl_syukkin2 = tk.Label(syukkin_window, text = '') lbl_syukkin1.pack() lbl_syukkin2.pack() thread1 = threading.Thread(target = syukkin_act) thread1.start() btn_return = tk.Button(syukkin_window, text='前の画面に戻る', command=return_main) btn_return.place(x = 280, y = 200) syukkin_window.mainloop() #メイン画面 main = tk.Tk() main.title('main') main.geometry('400x300') lbl1 = tk.Label(text = "行いたい操作のボタンを押してください") lbl1.place(x = 110, y = 70) time = tk.Label() time.place(x = 10, y = 10) timer() btn1 = tk.Button(text = "出勤", command = syukkin) btn1.place(x = 100, y = 100) btn2 = tk.Button(text ="退勤") btn2.place(x = 150, y =100) btn3 = tk.Button(text ="管理") btn3.place(x = 200, y =100) def syuryo(): connection.close main.destroy() btn4 = tk.Button(text = "終了", command = syuryo) btn4.place(x = 250, y = 100) lbl2 = tk.Label() main.mainloop()
回答2件
あなたの回答
tips
プレビュー