标签:update alt title state layer targe stringvar pac 最大
贴吧看到的一个求助题,大致需求是:3个人摇骰子,每人摇3次,点数之和最大的获胜,支持玩家名称输入。我觉得这个题目挺有意思的,做了个界面程序,欢迎大家交流指正~
1 #!usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 # author: *** 5 # date: 2020/06/10 6 7 from tkinter import * 8 import tkinter as tk 9 import random 10 import time 11 from threading import Thread 12 13 root = Tk() 14 root.geometry(‘620x660‘) 15 root.title(‘摇骰子‘) 16 sigpic = PhotoImage(file=‘signature.png‘) 17 shake_cup = PhotoImage(file=‘touzi/box.png‘) 18 crown = PhotoImage(file=‘touzi/win.png‘) 19 readystate = 3 20 playing = False 21 used_name = set() 22 result_dict = dict() 23 remain = 3 24 esv_A = StringVar() 25 esv_B = StringVar() 26 esv_C = StringVar() 27 28 pi_list = list() 29 for i in range(1, 7): 30 pi = PhotoImage(file=r‘touzi/t%s.png‘ % i) 31 pi_list.append(pi) 32 33 34 def set_name(cw, ew, nw): 35 global readystate 36 entryV = ew.get() 37 if entryV: 38 if entryV in used_name: 39 default = nw[‘text‘] 40 nw.config(text="名称已存在!") 41 cw.config(state=tk.DISABLED) 42 font_shake(nw, default) 43 cw.config(state=tk.ACTIVE) 44 else: 45 used_name.add(entryV) 46 nw.config(text=entryV) 47 ew.config(state=tk.DISABLED) 48 cw.config(state=tk.DISABLED) 49 readystate -= 1 50 if readystate == 0: 51 for i in "ABC": 52 eval(‘play_btn_%s.config(state=tk.ACTIVE, text="第一次机会")‘ % i) 53 54 55 def throw_touzi(pw, rw, nw): 56 global playing 57 global remain 58 if pw[‘text‘]=="第一次机会": 59 playing = True 60 pbStack.remove(pw) 61 for widget in pbStack: 62 widget.config(state=tk.DISABLED) 63 pw.config(state=tk.DISABLED, text=‘第二次机会‘) 64 elif pw[‘text‘]=="第二次机会": 65 pw.config(state=tk.DISABLED, text=‘第三次机会‘) 66 else: 67 playing = False 68 remain -= 1 69 pw.config(state=tk.DISABLED, text=‘play‘) 70 thread = Thread(target=change_img, args=[pw, rw, nw]) 71 thread.start() 72 73 74 def change_img(pw, rw, nw): 75 result_number = random.randint(1, 6) 76 ranum_list = list() 77 times = 5 78 while times: 79 ranum = random.randint(1, 6) 80 if ranum not in ranum_list: 81 ranum_list.append(ranum) 82 times = times - 1 83 for i in ranum_list: 84 time.sleep(0.3) 85 throw_label.config(image=pi_list[i-1]) 86 time.sleep(0.3) 87 throw_label.config(image=pi_list[result_number-1]) 88 time.sleep(0.5) 89 if rw[‘text‘] == "结果": 90 rw[‘text‘] = str(result_number) 91 else: 92 rw[‘text‘] = str(rw[‘text‘]) + "+%s" % result_number 93 time.sleep(0.5) 94 rw[‘text‘] = eval(rw[‘text‘]) 95 if pw[‘text‘] != "play": 96 pw.config(state=tk.ACTIVE) 97 if playing == False: 98 result_dict[nw[‘text‘]] = rw[‘text‘] 99 for widget in pbStack: 100 widget.config(state=tk.ACTIVE) 101 if not remain: 102 result_list = sorted(result_dict.items(), reverse=True, key=lambda rt: rt[1]) 103 if result_list[0][1] == result_list[1][1]: 104 if result_list[1][1] == result_list[2][1]: 105 throw_winner[‘text‘] = ">> 平局 <<" 106 else: 107 winner = result_list[0][0] + ", " + result_list[1][0] 108 throw_winner[‘text‘] = "Winner: %s" % winner 109 else: 110 winner = result_list[0][0] 111 throw_winner[‘text‘] = "Winner: %s" % winner 112 throw_label[‘image‘] = crown 113 reset_btn.config(state=tk.ACTIVE, text=‘重新开始‘, relief=‘raised‘) 114 115 116 def font_shake(nw, default): 117 nw.config(foreground=‘red‘) 118 for i in range(5): 119 if i%2 == 0: 120 time.sleep(0.05) 121 nw.config(anchor=‘n‘) 122 else: 123 time.sleep(0.05) 124 nw.config(anchor=‘s‘) 125 root.update() 126 nw.config(anchor=‘c‘) 127 root.update() 128 time.sleep(0.5) 129 nw.config(text=default, foreground=‘black‘) 130 131 132 def restart(): 133 global readystate, used_name, result_dict, remain, pbStack 134 throw_label[‘image‘] = shake_cup 135 readystate = 3 136 used_name = set() 137 result_dict = dict() 138 remain = 3 139 pbStack = {play_btn_A, play_btn_B, play_btn_C} 140 reset_btn.config(state=tk.DISABLED, relief=‘flat‘, text=‘‘) 141 for i in "ABC": 142 eval(‘name_%s.config(text="player %s")‘ % (i, i)) 143 eval(‘entry_%s.config(state=tk.NORMAL)‘ % i) 144 eval(‘esv_%s.set("")‘ % i) 145 eval(‘confirm_btn_%s.config(state=tk.ACTIVE)‘ % i) 146 eval(‘result_%s.config(text="结果")‘ % i) 147 throw_winner[‘text‘] = "" 148 149 150 box_frame_A = Frame(root) 151 box_frame_A.grid(column=1, ipadx=3) 152 name_A = Label(box_frame_A, text=‘player A‘, height=2) 153 name_A.pack() 154 signature_A = Label(box_frame_A, image=sigpic) 155 signature_A.pack() 156 play_btn_A = Button(box_frame_A, text=‘play‘, command=lambda : throw_touzi(play_btn_A, result_A, name_A)) 157 play_btn_A.pack(side=BOTTOM, pady=3) 158 result_A = Label(box_frame_A, text=‘结果‘) 159 result_A.pack(side=BOTTOM) 160 entry_A = Entry(box_frame_A, textvariable=esv_A) 161 entry_A.pack(side=LEFT, padx=5) 162 confirm_btn_A = Button(box_frame_A, text=‘确定‘, command=lambda : set_name(confirm_btn_A, entry_A, name_A)) 163 confirm_btn_A.pack(side=LEFT) 164 165 center_frame = Frame(root) 166 center_frame.grid(row=1, column=1, pady=20) 167 throw_label = Label(center_frame, image=shake_cup) 168 throw_label.pack() 169 throw_winner = Label(center_frame) 170 throw_winner.pack() 171 reset_btn = Button(center_frame, state=tk.DISABLED, relief=‘flat‘, command=restart) 172 reset_btn.pack() 173 174 box_frame_B = Frame(root) 175 box_frame_B.grid(row=3, padx=10) 176 name_B = Label(box_frame_B, text=‘player B‘, height=2) 177 name_B.pack() 178 signature_B = Label(box_frame_B, image=sigpic) 179 signature_B.pack() 180 play_btn_B = Button(box_frame_B, text=‘play‘, command=lambda : throw_touzi(play_btn_B, result_B, name_B)) 181 play_btn_B.pack(side=BOTTOM) 182 result_B = Label(box_frame_B, text=‘结果‘) 183 result_B.pack(side=BOTTOM) 184 entry_B = Entry(box_frame_B, textvariable=esv_B) 185 entry_B.pack(side=LEFT, padx=5) 186 confirm_btn_B = Button(box_frame_B, text=‘确定‘, command=lambda : set_name(confirm_btn_B, entry_B, name_B)) 187 confirm_btn_B.pack(side=LEFT) 188 189 190 box_frame_C = Frame(root) 191 box_frame_C.grid(row=3, column=2, padx=10) 192 name_C = Label(box_frame_C, text=‘player C‘, height=2) 193 name_C.pack() 194 signature_C = Label(box_frame_C, image=sigpic) 195 signature_C.pack() 196 play_btn_C = Button(box_frame_C, text=‘play‘, command=lambda : throw_touzi(play_btn_C, result_C, name_C)) 197 play_btn_C.pack(side=BOTTOM) 198 result_C = Label(box_frame_C, text=‘结果‘) 199 result_C.pack(side=BOTTOM) 200 entry_C = Entry(box_frame_C, textvariable=esv_C) 201 entry_C.pack(side=LEFT, padx=5) 202 confirm_btn_C = Button(box_frame_C, text=‘确定‘, command=lambda : set_name(confirm_btn_C, entry_C, name_C)) 203 confirm_btn_C.pack(side=LEFT) 204 205 206 if __name__ == ‘__main__‘: 207 pbStack = {play_btn_A, play_btn_B, play_btn_C} 208 for i in "ABC": 209 eval(‘play_btn_%s.config(state=tk.DISABLED)‘ % i) 210 root.mainloop()
程序界面截图:
1.初始界面
2.就绪界面
3.结果界面
标签:update alt title state layer targe stringvar pac 最大
原文地址:https://www.cnblogs.com/ywfft/p/a20200702.html