TKinter
Python 的 GUI 库非常多,之所以选择 Tkinter,一是最为简单,二是自带库,不需下载安装,随时使用,跨平台兼容性非常好,三则是从需求出发的,Python 在实际应用中极少用于开发复杂的桌面应用,毕竟,Python 的各种 GUI 工具包都“一般得很”,不具备优势。
贴吧看到的一个求助题,大致需求是:3个人摇骰子,每人摇3次,点数之和最大的获胜,支持玩家名称输入。我觉得这个题目挺有意思的,做了个界面程序,欢迎大家交流指正~
#!usr/bin/env python # -*- coding: utf-8 -*- # author: *** # date: 2020/06/10 from tkinter import * import tkinter as tk import random import time from threading import Thread root = Tk() root.geometry('620x660') root.title('摇骰子') sigpic = PhotoImage(file='signature.png') shake_cup = PhotoImage(file='touzi/box.png') crown = PhotoImage(file='touzi/win.png') readystate = 3 playing = False used_name = set() result_dict = dict() remain = 3 esv_A = StringVar() esv_B = StringVar() esv_C = StringVar() pi_list = list() for i in range(1, 7): pi = PhotoImage(file=r'touzi/t%s.png' % i) pi_list.append(pi) def set_name(cw, ew, nw): global readystate entryV = ew.get() if entryV: if entryV in used_name: default = nw['text'] nw.config(text="名称已存在!") cw.config(state=tk.DISABLED) font_shake(nw, default) cw.config(state=tk.ACTIVE) else: used_name.add(entryV) nw.config(text=entryV) ew.config(state=tk.DISABLED) cw.config(state=tk.DISABLED) readystate -= 1 if readystate == 0: for i in "ABC": eval('play_btn_%s.config(state=tk.ACTIVE, text="第一次机会")' % i) def throw_touzi(pw, rw, nw): global playing global remain if pw['text']=="第一次机会": playing = True pbStack.remove(pw) for widget in pbStack: widget.config(state=tk.DISABLED) pw.config(state=tk.DISABLED, text='第二次机会') elif pw['text']=="第二次机会": pw.config(state=tk.DISABLED, text='第三次机会') else: playing = False remain -= 1 pw.config(state=tk.DISABLED, text='play') thread = Thread(target=change_img, args=[pw, rw, nw]) thread.start() def change_img(pw, rw, nw): result_number = random.randint(1, 6) ranum_list = list() times = 5 while times: ranum = random.randint(1, 6) if ranum not in ranum_list: ranum_list.append(ranum) times = times - 1 for i in ranum_list: time.sleep(0.3) throw_label.config(image=pi_list[i-1]) time.sleep(0.3) throw_label.config(image=pi_list[result_number-1]) time.sleep(0.5) if rw['text'] == "结果": rw['text'] = str(result_number) else: rw['text'] = str(rw['text']) + "+%s" % result_number time.sleep(0.5) rw['text'] = eval(rw['text']) if pw['text'] != "play": pw.config(state=tk.ACTIVE) if playing == False: result_dict[nw['text']] = rw['text'] for widget in pbStack: widget.config(state=tk.ACTIVE) if not remain: result_list = sorted(result_dict.items(), reverse=True, key=lambda rt: rt[1]) if result_list[0][1] == result_list[1][1]: if result_list[1][1] == result_list[2][1]: throw_winner['text'] = " 平局 <<" else: winner = result_list[0][0] + ", " + result_list[1][0] throw_winner['text'] = "Winner: %s" % winner else: winner = result_list[0][0] throw_winner['text'] = "Winner: %s" % winner throw_label['image'] = crown reset_btn.config(state=tk.ACTIVE, text='重新开始', relief='raised') def font_shake(nw, default): nw.config(foreground='red') for i in range(5): if i%2 == 0: time.sleep(0.05) nw.config(anchor='n') else: time.sleep(0.05) nw.config(anchor='s') root.update() nw.config(anchor='c') root.update() time.sleep(0.5) nw.config(text=default, foreground='black') def restart(): global readystate, used_name, result_dict, remain, pbStack throw_label['image'] = shake_cup readystate = 3 used_name = set() result_dict = dict() remain = 3 pbStack = {play_btn_A, play_btn_B, play_btn_C} reset_btn.config(state=tk.DISABLED, relief='flat', text='') for i in "ABC": eval('name_%s.config(text="player %s")' % (i, i)) eval('entry_%s.config(state=tk.NORMAL)' % i) eval('esv_%s.set("")' % i) eval('confirm_btn_%s.config(state=tk.ACTIVE)' % i) eval('result_%s.config(text="结果")' % i) throw_winner['text'] = "" box_frame_A = Frame(root) box_frame_A.grid(column=1, ipadx=3) name_A = Label(box_frame_A, text='player A', height=2) name_A.pack() signature_A = Label(box_frame_A, image=sigpic) signature_A.pack() play_btn_A = Button(box_frame_A, text='play', command=lambda : throw_touzi(play_btn_A, result_A, name_A)) play_btn_A.pack(side=BOTTOM, pady=3) result_A = Label(box_frame_A, text='结果') result_A.pack(side=BOTTOM) entry_A = Entry(box_frame_A, textvariable=esv_A) entry_A.pack(side=LEFT, padx=5) confirm_btn_A = Button(box_frame_A, text='确定', command=lambda : set_name(confirm_btn_A, entry_A, name_A)) confirm_btn_A.pack(side=LEFT) center_frame = Frame(root) center_frame.grid(row=1, column=1, pady=20) throw_label = Label(center_frame, image=shake_cup) throw_label.pack() throw_winner = Label(center_frame) throw_winner.pack() reset_btn = Button(center_frame, state=tk.DISABLED, relief='flat', command=restart) reset_btn.pack() box_frame_B = Frame(root) box_frame_B.grid(row=3, padx=10) name_B = Label(box_frame_B, text='player B', height=2) name_B.pack() signature_B = Label(box_frame_B, image=sigpic) signature_B.pack() play_btn_B = Button(box_frame_B, text='play', command=lambda : throw_touzi(play_btn_B, result_B, name_B)) play_btn_B.pack(side=BOTTOM) result_B = Label(box_frame_B, text='结果') result_B.pack(side=BOTTOM) entry_B = Entry(box_frame_B, textvariable=esv_B) entry_B.pack(side=LEFT, padx=5) confirm_btn_B = Button(box_frame_B, text='确定', command=lambda : set_name(confirm_btn_B, entry_B, name_B)) confirm_btn_B.pack(side=LEFT) box_frame_C = Frame(root) box_frame_C.grid(row=3, column=2, padx=10) name_C = Label(box_frame_C, text='player C', height=2) name_C.pack() signature_C = Label(box_frame_C, image=sigpic) signature_C.pack() play_btn_C = Button(box_frame_C, text='play', command=lambda : throw_touzi(play_btn_C, result_C, name_C)) play_btn_C.pack(side=BOTTOM) result_C = Label(box_frame_C, text='结果') result_C.pack(side=BOTTOM) entry_C = Entry(box_frame_C, textvariable=esv_C) entry_C.pack(side=LEFT, padx=5) confirm_btn_C = Button(box_frame_C, text='确定', command=lambda : set_name(confirm_btn_C, entry_C, name_C)) confirm_btn_C.pack(side=LEFT) if __name__ == '__main__': pbStack = {play_btn_A, play_btn_B, play_btn_C} for i in "ABC": eval('play_btn_%s.config(state=tk.DISABLED)' % i) root.mainloop()
程序界面截图:
1.初始界面
2.就绪界面
3.结果界面
总结
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 炉石传说月初最强卡组有哪些 2024月初最强上分卡组推荐
- 狼人杀亮相原生鸿蒙之夜 假面科技强势登陆华为生态
- 12小时光线挑战!AI画质专家才是大平层首选
- 2024游戏IP报告:1~9月规模1960亿 68%用户愿为之付费
- 群星.2024-今夜一起为爱鼓掌电视剧原声带【相信音乐】【FLAC分轨】
- BIGFOUR.2013-大家利事【寰亚】【WAV+CUE】
- 李美凤.1992-情深透全情歌集【EMI百代】【WAV+CUE】
- 田震2024-《时光音乐会》[金峰][WAV+CUE]
- 群星《监听天碟3》[LECD]限量版[WAV+CUE]
- 心妤《声如夏花HQ》头版限量编号[WAV+CUE]
- 群星《摇滚五杰》[低速原抓WAV+CUE][1.1G]
- 群星 《2024好听新歌30》十倍音质 U盘音乐 [WAV+分轨]
- 群星《试音草原·女声篇》经典蒙古民歌[WAV+CUE][1G]
- 陈慧娴《永远是你的朋友》头版限量编号MQA-UHQCD2024[低速原抓WAV+CUE]
- 曼丽·女人三十《如果·爱》限量1:1母盘直刻[低速原抓WAV+CUE]