youngfan007 发表于 2018-8-10 13:21:08

python学习之GUI(Tkinter)

import random  
import Tkinter as tk
  
window=tk.Tk()
  

  
maxNo=10
  
score=0
  
rounds=0
  

  
def buttonClick():
  
    global score
  
    global rounds
  
    try:
  
      guess=int(guessBox.get())
  
      if 0<guess<=maxNo:
  
            result=random.randrange(1,maxNo+1)
  
            if guess==result:
  
                score=score+1
  
            rounds+=1
  
      else:
  
            result="Entry not valid"
  
    except:
  
      result="Entry not valid"
  

  
    resultLabel.config(text=result)
  
    scoreLabel.config(text=str(score)+"/"+str(rounds))
  
    guessBox.delete(0,tk.END)
  

  
guessLabel=tk.Label(window,text="Enter a number from 1 to"+str(maxNo))
  
guessBox=tk.Entry(window)
  
resultLabel=tk.Label(window)
  
scoreLabel=tk.Label(window)
  
button=tk.Button(window,text="guess",command=buttonClick)
  

  
guessLabel.pack()
  
guessBox.pack()
  
resultLabel.pack()
  
scoreLabel.pack()
  
button.pack()
  

  
window.mainloop()'''
  

  

  
'''import Tkinter as tk
  
window=tk.Tk()
  

  
slider=tk.Scale(window,from_=0,to=100)
  
slider.pack()
  
tk.mainloop()
页: [1]
查看完整版本: python学习之GUI(Tkinter)