python tkinterによる点呼ウィジェット機能の実例コード
コードは以下の通りです。
ここでは、python tkinterの点呼小プログラム機能に基づく実例コードについての記事を紹介します。python tkinter点呼小プログラムの内容については、以前の文章を検索したり、下記の関連記事を引き続き閲覧したりしてください。これからもよろしくお願いします。
import datetime
import json
import os
import random
import tkinter as tk
import openpyxl
#
excel_file_path = " .xlsx"# .xlsx
#
excel_sheet = "Sheet1"
#
file_path = "name_record.json"
#
class Call_Name:
#
_namelist = None
#
def __init__(self):
wb = openpyxl.load_workbook(excel_file_path)
get_sheet = wb[excel_sheet]
list_data = []
first_row = True
try:
for row in get_sheet:
#
if first_row:
first_row = False
continue
#
for lab in row:
list_data.append(lab.value)
break
except:
list_data = [' ?']
self._namelist = list_data
#
def _re_reset(self):
#
dls = []
dy = str(datetime.datetime.today().date())
dls.append(dy)
#
record = {}
for i in self._namelist:
record[i] = 0
name_record = {"date": dls, "last_use": dy, "record": record}
return name_record
#
def mk_record(self):
jf_dict = self._re_reset()
with open(file=file_path, mode='w', encoding='utf-8') as jf:
json.dump(jf_dict, jf, indent=2, sort_keys=True, ensure_ascii=False)
#
def read_record(self):
with open(file=file_path, mode='r', encoding='utf-8') as jf:
jf_data = json.load(jf)
return jf_data
#
def call_name(self):
ll = len(self._namelist)
num = random.randint(1, ll)
call_name = self._namelist[num - 1]
return call_name
# -----warning!!!
def re_mod(self, name=None, re_date=None):
jf_data = self.read_record()
record = jf_data["record"]
use_date = list(jf_data["date"])
if name:
v = record[name] + 1
jf_data["record"][name] = v
if re_date:
use_date.append(re_date)
jf_data["date"] = use_date
jf_data["last_use"] = re_date
with open(file=file_path, mode='w', encoding='utf-8') as jf:
json.dump(jf_data, jf, indent=2, sort_keys=True, ensure_ascii=False)
#
def re_times(self):
times_list = []
record = self.read_record()["record"]
for k, v in record.items():
times_list.append(v)
return times_list #
#
def call_times(self, name):
record = self.read_record()["record"]
return record[name]
#
class ForGUI:
#
re_date = None
def __init__(self):
try:
cname = Call_Name()
except:
msg["text"] = " :%s" % excel_file_path
msg.update()
else:
try:
record = cname.read_record() #
except:
cname.mk_record() #
record = cname.read_record()
self.re_date = record["date"] #
# 5
if len(self.re_date) > 5:
cname.mk_record() #
# , :
def start(self):
cname = Call_Name()
times = cname.re_times() #
max_times = max(times) #
min_times = min(times) #
td = str(datetime.datetime.today().date()) #
if not td in self.re_date:
cname.re_mod(re_date=td)
while True:
name = cname.call_name() #
times = cname.call_times(name) #
if max_times != min_times:
if times != max_times:
cname.re_mod(name=name) #
msg["text"] = name
msg.update()
break
else:
continue
else:
cname.re_mod(name=name) #
msg["text"] = name
msg.update()
break
#
def open_name_excel(self):
try:
os.system("start %s&exit" % excel_file_path)
except:
msg["text"] = '
r(st)q
'
msg["font"] = ('', 18)
msg.update()
#
def open_record(self):
try:
os.system("start %s&exit" % file_path)
except:
msg["text"] = '
r(st)q
'
msg["font"] = ('', 18)
msg.update()
#
def get_name_list(self):
try:
wb = openpyxl.load_workbook(excel_file_path)
get_sheet = wb[excel_sheet]
except:
msg["text"] = " :%s" % excel_file_path
msg.update()
else:
list_data = []
first_row = True
for row in get_sheet:
#
if first_row:
first_row = False
continue
#
for lab in row:
list_data.append(lab.value)
break
return list_data
#
root = tk.Tk(className=" ")
#
setWidth = 300
setHeight = 180
#
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
x = int((screenWidth - setWidth) / 2)
y = int((screenHeight - setHeight) / 2)
#
root.geometry("%sx%s+%s+%s" % (setWidth, setHeight, x, y))
#
root.resizable(0, 0)
#
m1 = tk.Menu(root, tearoff=False)
# ,
MenuBar = tk.Menu(m1, tearoff=False)
MenuBar.add_command(label=' ', command=ForGUI().open_name_excel)
MenuBar.add_command(label=' ', command=ForGUI().open_record)
MenuBar.add_command(label=' ', command=Call_Name().mk_record)
#
m1.add_cascade(label=' ', menu=MenuBar)
m1.add_command(label='Exit', command=root.quit)
#
root.config(menu=m1)
#
f1 = tk.Frame(root, bd=1, height=150, width=200)
pass
f1.pack(pady=10)
msg = tk.Label(f1, text="
(●ˇ∀ˇ●)
", fg="green") #
msg.pack(pady=10)
msg["font"] = ('', 18)
f2 = tk.Frame(root)
f2.pack(pady=10)
#
name_list = ForGUI().get_name_list()
def info():
t = random.randint(1, len(name_list))
msg["font"] = ('', 44)
msg["text"] = (name_list[t - 1])
def bt_listen():
while bt["state"] == 'normal':
bt['command'] = bt_start
bt['text'] = " "
root.after(500, info)
bt.update()
if bt['text'] == " ":
break
def bt_start():
bt['command'] = bt_listen
bt['text'] = " "
fg = ForGUI()
root.after(500, fg.start)
bt.update()
#
bt = tk.Button(f2, text=' ', stat="normal", command=bt_listen, bd=4, width=20, font=18)
bt.pack()
root.mainloop()
締め括りをつけるここでは、python tkinterの点呼小プログラム機能に基づく実例コードについての記事を紹介します。python tkinter点呼小プログラムの内容については、以前の文章を検索したり、下記の関連記事を引き続き閲覧したりしてください。これからもよろしくお願いします。