文章目录
前言
-
- 前言
- 关于Tkinter
- 实现效果
- 以下是全部代码:
学习Python实践,多练习才是悟出道理的唯一,对于一个初级学习python的人,这将是一个非常简单而有趣的内容; 代码非常简单,主要是:随机函数–需要导入random模块与条件语句的一个简单实用
Python+Tkinter图形化界面设计代码界面实现猜拳游戏。可以打包成exe放在桌面玩。
关于Tkinter当前流行的计算机桌面应用程序大多数为图形化用户界面(Graphic User Interface,GUI) Python自带了tkinter 模块,实质上是一种流行的面向对象的GUI工具包 TK 的Python编程接口,提供了快速便利地创建GUI应用程序的方法。其图像化编程的基本步骤通常包括
https://baike.baidu.com/item/Tkinter/4538615或上博客找找看吧。
# @Author : 王同学 import random import tkinter as tk import threading import multiprocessing def get_data(): # 用户出拳 user = str(var.get()) # 电脑出拳 head = ['石头','剪刀','布'] windows = random.choice(head) # 进行判断 if user == windows: print('平局') text.insert(tk.INSERT,'猜拳结束=====》: 平局,大战三百回合。' + '\n') text.yview_moveto(1) text.update() elif (user == '石头' and windows == '剪刀') or (user == '剪刀' and windows == '布') or (user == '布' and windows == '石头'): print('恭喜你胜利了') text.insert(tk.INSERT,'猜拳结束=====》: 恭喜你胜利了,继续猜拳。' + '\n') text.yview_moveto(1) text.update() else: print('你输了') text.insert(tk.INSERT, '猜拳结束=====》: 很不幸你输了,不服再来。' + '\n') text.yview_moveto(1) text.update() # 清空文本 def exit_games(): text.delete(1.0,tk.END) def thred(): thred = threading.Thread(target=get_data) thred.start() # print(var.get()) if __name__ == '__main__': # tkinter界面实现 windoms = tk.Tk() # 窗口标题 windoms.title('猜拳游戏') # 窗口大小位置 windoms.geometry('800x600+500+200') # 文本 labe = tk.Label(windoms,text='猜拳游戏',font=('微软雅黑',30)) labe.place(x=300,y=1) labee = tk.Label(windoms,text='请输入你要出的(石头剪刀布):',font=('微软雅黑',20)) labee.place(x=10,y=90) # 文本框 text = tk.Text(windoms,height=30,width=90,bg='light cyan') text.place(x=150,y=150) # 输入框 var = tk.StringVar() new_input = tk.Entry(windoms,font=('微软雅黑',20),bg='light cyan',textvariable=var) new_input.place(x=370,y=80,height=60,width=410) # 按钮 button = tk.Button(windoms,text='开始猜拳',font=('微软雅黑',20),bg='pink',bd=5,command=thred) button.place(x=10,y=150) # 按钮 buttonn = tk.Button(windoms,text='退出游戏',font=('微软雅黑',20),bg='pink',bd=5,command=windoms.quit) buttonn.place(x=10,y=400) # 按钮 buttonn = tk.Button(windoms,text='清空文本',font=('微软雅黑',20),bg='pink',bd=5,command=exit_games) buttonn.place(x=10,y=250) # 显示窗口 windoms.mainloop()