最后活跃于 1753683930

修订 53ea86cc5aad5b1d6a5f5c1db9fa78762223e8c8

TKNG-v2.py 原始文件
1"""
2TKNumGuess
3Use with python
4Dont Forget to install TK into your python installation using:
5pip install tk
6"""
7import tkinter as tk, tkinter.messagebox, random
8global awns
9awns,nume,window, = random.randint(0,100),-1,tk.Tk()
10
11def guess():
12 if nume == -1:
13 tkinter.messagebox.showerror('Alert!','Please choose a number!')
14 else:
15 if nume == awns:
16 tkinter.messagebox.showinfo('Game','You win!')
17 window.destroy()
18 else:
19 if nume > awns:
20 tkinter.messagebox.showinfo('Game','The Number is lower. Try again!')
21 else:
22 tkinter.messagebox.showinfo('Game','The Number is higher. Try again!')
23def print_selection(num):
24 global nume
25 nume = int(num)
26hello,s,button = tk.Label(text="Guess the Number Between 0 and 100"),tk.Scale(window, from_=0, to=100, orient=tk.HORIZONTAL, length=200, showvalue=1,tickinterval=20, resolution=1, command=print_selection),tk.Button(text="Guess!",command=guess)
27window.title("TKNumGuess")
28window.geometry("240x100")
29hello.pack()
30s.pack()
31button.pack()
32tk.mainloop()
tknumguess.py 原始文件
1from tkinter import *
2import tkinter as tk
3import tkinter.messagebox
4import random
5class Window(Frame):
6
7 def __init__(self, master=None):
8 Frame.__init__(self, master)
9 self.master = master
10 self.e = ""
11 self.num = random.randrange(1,100)
12 # widget can take all window
13 self.pack(fill=BOTH, expand=1)
14
15 # create button, link it to clickExitButton()
16 exitButton = Button(self, text="Guess" ,command=self.clickExitButton)
17
18 # place button at (0,0)
19 exitButton.place(x=0, y=60)
20 self.s = tk.Scale(self, label='none', from_=0, to=100, orient=tk.HORIZONTAL, length=500, showvalue=0,tickinterval=10, resolution=1, command=self.print_selection)
21 self.s.pack()
22 def print_selection(self,v):
23 print(v)
24 self.e = v
25 self.s.config(label = v)
26 def clickExitButton(self):
27 print(self.e)
28 if self.e == "":
29 tkinter.messagebox.showerror('Error','You didn\'t even choose anything!')
30 else:
31 if int(self.e) == self.num:
32 tkinter.messagebox.showinfo('game','you got it!')
33 exit("you won!")
34 else:
35 if int(self.e) > self.num:
36 tkinter.messagebox.showwarning('note','lower')
37 else:
38 tkinter.messagebox.showwarning('note','higher')
39
40root = Tk()
41app = Window(root)
42root.wm_title("Number Guesser made with Tkinter")
43root.geometry("500x85")
44root.mainloop()