【发布时间】:2020-12-16 11:59:48
【问题描述】:
我是编程新手,一直在尝试掌握函数的窍门,并开始探索 Tkinter。我按照这个关于石头、纸、剪刀的教程,我有一个重置按钮,当用户点击它时,它会清除结果和用户输入的内容作为他们的选择,除了它似乎没有清除计算机的选择,即计算机选择rock,点击reset,电脑再次选择rock,reset,等等……所以我必须点击exit,然后运行程序才能得到不同的结果。
我尝试在def Reset() 和comp_pick.set("") 中分配comp_pick = None,因为我像这样清除了user_pick。我只是想弄清楚我错过了什么。
#import library
from tkinter import *
import random
#initialize window set up
root = Tk()
root.geometry('500x500')
root.resizable(0,0)
root.title('DataFlair-Rock,Paper,Scissors')
root.config(bg ='pink3')
#heading set up
Label(root, text = 'Rock, Paper ,Scissors' , font='arial 20 bold', bg = 'pink2').pack()
##user choice set up
user_take = StringVar()
Label(root, text = 'choose any one: rock, paper ,scissors' , font='arial 15 bold', bg = 'pink2').place(x = 70,y=70)
Entry(root, font = 'arial 15', textvariable = user_take , bg = 'pink2').place(x=90 , y = 130)
#computer choice using the randint function
comp_pick = random.randint(1,3)
if comp_pick == 1:
comp_pick = 'rock'
elif comp_pick ==2:
comp_pick = 'paper'
else:
comp_pick = 'scissors'
##function to play
Result = StringVar()
def play():
user_pick = user_take.get()
if user_pick == comp_pick:
Result.set('It is a tie. You both have selected ' + user_pick +'.')
elif user_pick == 'rock' and comp_pick == 'paper':
Result.set('You loose! Computer selected paper.')
elif user_pick == 'rock' and comp_pick == 'scissors':
Result.set('You win! Computer selected scissors.')
elif user_pick == 'paper' and comp_pick == 'scissors':
Result.set('You loose! Computer selected scissors.')
elif user_pick == 'paper' and comp_pick == 'rock':
Result.set('You win! Computer selected rock.')
elif user_pick == 'scissors' and comp_pick == 'rock':
Result.set('You loose! Computer selected rock.')
elif user_pick == 'scissors' and comp_pick == 'paper':
Result.set('You win! Computer selected paper.')
else:
Result.set('invalid: choose any one -- rock, paper, scissors')
##fun to reset
def Reset():
Result.set("")
user_take.set("")
comp_pick.set("")
##fun to exit
def Exit():
root.destroy()
###### button
Entry(root, font = 'arial 10 bold', textvariable = Result, bg ='pink2',width = 50,).place(x=25, y = 250)
Button(root, font = 'arial 13 bold', text = 'PLAY' ,padx =5,bg ='pink4' ,command = play).place(x=150,y=190)
Button(root, font = 'arial 13 bold', text = 'RESET' ,padx =5,bg ='pink4' ,command = Reset).place(x=70,y=310)
Button(root, font = 'arial 13 bold', text = 'EXIT' ,padx =5,bg ='pink4' ,command = Exit).place(x=230,y=310)
root.mainloop()
【问题讨论】:
标签: python