原来看到石头剪刀布就想通过Tkinter库来实现,今天用了一段时间来实现,本来函数想只通过一个来实现,但奈何本人新手,人菜瘾又大弄了一个下午没有成功,只好放弃了,现在通过三个函数来实现,发出来想请各位大佬指点一番,感谢大家的指教我会进行学习的。
1.tkinter模块的导入及创建py文件,创建石头剪刀布的小游戏:import random import tkinter as tk win = tk.Tk() # 实例化窗口 win.title('石头剪刀布') # 窗口的标题 win.geometry('400x250') # 窗口的尺寸2. 创建标签及申请变量:
# 实例化三个标签 tk.Label(win, text='石头剪刀布您出的是 :').place(x=40, y=40) tk.Label(win, text='石头剪刀布电脑出了 :').place(x=40, y=80) tk.Label(win, text='石头剪刀布的结果是 :').place(x=40, y=120) # 实例化三个字符串变量 var = tk.StringVar() var1 = tk.StringVar() var2 = tk.StringVar()3. 建立点击函数:
# 写函数(新人上手重复代码没有解决请多多见谅!!!) def one(): planyer = 1 pout = "石头" computer = random.randint(1, 3) if (planyer == 1 and computer == 2) or (planyer == 2 and computer == 3) or (planyer == 3 and computer == 1): result = "您战胜了电脑,就这?" elif planyer == computer: result = "您和电脑打平,就差一点了!" else: result = "电脑战胜了您,再来一把!!!" if computer == 1: cout = "石头" elif computer == 2: cout = "剪刀" else: cout = "布" var.set(pout) var1.set(cout) var2.set(result) # 写函数(新人上手重复代码没有解决请多多见谅!!!) def one1(): planyer = 2 pout = "剪刀" computer = random.randint(1, 3) if (planyer == 1 and computer == 2) or (planyer == 2 and computer == 3) or (planyer == 3 and computer == 1): result = "您战胜了电脑,就这?" elif planyer == computer: result = "您和电脑打平,就差一点了!" else: result = "电脑战胜了您,再来一把!!!" if computer == 1: cout = "石头" elif computer == 2: cout = "剪刀" else: cout = "布" var.set(pout) var1.set(cout) var2.set(result) # 写函数(新人上手重复代码没有解决请多多见谅!!!) def one2(): planyer = 3 pout = "布" # 生成随机数 computer = random.randint(1, 3) # 判断函数 if (planyer == 1 and computer == 2) or (planyer == 2 and computer == 3) or (planyer == 3 and computer == 1): result = "您战胜了电脑,就这?" elif planyer == computer: result = "您和电脑打平,就差一点了!" else: result = "电脑战胜了您,再来一把!!!" # 判断函数 if computer == 1: cout = "石头" elif computer == 2: cout = "剪刀" else: cout = "布" var.set(pout) var1.set(cout) var2.set(result)
ps:就是这里一直没有解决!!
4.创建按钮,用于触发函数,循环程序。# 实例化三个标签 tk.Label(win, width=21, height=1, bg='white', textvariable=var).place(x=220, y=40) tk.Label(win, width=21, height=1, bg='white', textvariable=var1).place(x=220, y=80) tk.Label(win, width=21, height=1, bg='white', textvariable=var2).place(x=220, y=120) # 创建三个按钮 tk.Button(win, text='石头', width=12, height=2, command=one).place(x=30, y=180) tk.Button(win, text='剪刀', width=12, height=2, command=one1).place(x=150, y=180) tk.Button(win, text='布', width=12, height=2, command=one2).place(x=270, y=180) win.mainloop()运行结果:
全部代码:
import random import tkinter as tk win = tk.Tk() # 实例化窗口 win.title('石头剪刀布') # 窗口的标题 win.geometry('400x250') # 窗口的尺寸 # 实例化三个标签 tk.Label(win, text='石头剪刀布您出的是 :').place(x=40, y=40) tk.Label(win, text='石头剪刀布电脑出了 :').place(x=40, y=80) tk.Label(win, text='石头剪刀布的结果是 :').place(x=40, y=120) # 实例化三个字符串变量 var = tk.StringVar() var1 = tk.StringVar() var2 = tk.StringVar() # 写函数(新人上手重复代码没有解决请多多见谅!!!) def one(): planyer = 1 pout = "石头" computer = random.randint(1, 3) if (planyer == 1 and computer == 2) or (planyer == 2 and computer == 3) or (planyer == 3 and computer == 1): result = "您战胜了电脑,就这?" elif planyer == computer: result = "您和电脑打平,就差一点了!" else: result = "电脑战胜了您,再来一把!!!" if computer == 1: cout = "石头" elif computer == 2: cout = "剪刀" else: cout = "布" var.set(pout) var1.set(cout) var2.set(result) # 写函数(新人上手重复代码没有解决请多多见谅!!!) def one1(): planyer = 2 pout = "剪刀" computer = random.randint(1, 3) if (planyer == 1 and computer == 2) or (planyer == 2 and computer == 3) or (planyer == 3 and computer == 1): result = "您战胜了电脑,就这?" elif planyer == computer: result = "您和电脑打平,就差一点了!" else: result = "电脑战胜了您,再来一把!!!" if computer == 1: cout = "石头" elif computer == 2: cout = "剪刀" else: cout = "布" var.set(pout) var1.set(cout) var2.set(result) # 写函数(新人上手重复代码没有解决请多多见谅!!!) def one2(): planyer = 3 pout = "布" # 生成随机数 computer = random.randint(1, 3) # 判断函数 if (planyer == 1 and computer == 2) or (planyer == 2 and computer == 3) or (planyer == 3 and computer == 1): result = "您战胜了电脑,就这?" elif planyer == computer: result = "您和电脑打平,就差一点了!" else: result = "电脑战胜了您,再来一把!!!" # 判断函数 if computer == 1: cout = "石头" elif computer == 2: cout = "剪刀" else: cout = "布" var.set(pout) var1.set(cout) var2.set(result) # 实例化三个标签 tk.Label(win, width=21, height=1, bg='white', textvariable=var).place(x=220, y=40) tk.Label(win, width=21, height=1, bg='white', textvariable=var1).place(x=220, y=80) tk.Label(win, width=21, height=1, bg='white', textvariable=var2).place(x=220, y=120) # 创建三个按钮 tk.Button(win, text='石头', width=12, height=2, command=one).place(x=30, y=180) tk.Button(win, text='剪刀', width=12, height=2, command=one1).place(x=150, y=180) tk.Button(win, text='布', width=12, height=2, command=one2).place(x=270, y=180) win.mainloop()
求各位大佬可以帮忙看一下,救救孩子啊!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)