swee revidoval tento gist 4 months ago. Přejít na revizi
2 files changed, 76 insertions
TKNG-v2.py(vytvořil soubor)
| @@ -0,0 +1,32 @@ | |||
| 1 | + | """ | |
| 2 | + | TKNumGuess | |
| 3 | + | Use with python | |
| 4 | + | Dont Forget to install TK into your python installation using: | |
| 5 | + | pip install tk | |
| 6 | + | """ | |
| 7 | + | import tkinter as tk, tkinter.messagebox, random | |
| 8 | + | global awns | |
| 9 | + | awns,nume,window, = random.randint(0,100),-1,tk.Tk() | |
| 10 | + | ||
| 11 | + | def 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!') | |
| 23 | + | def print_selection(num): | |
| 24 | + | global nume | |
| 25 | + | nume = int(num) | |
| 26 | + | hello,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) | |
| 27 | + | window.title("TKNumGuess") | |
| 28 | + | window.geometry("240x100") | |
| 29 | + | hello.pack() | |
| 30 | + | s.pack() | |
| 31 | + | button.pack() | |
| 32 | + | tk.mainloop() | |
tknumguess.py(vytvořil soubor)
| @@ -0,0 +1,44 @@ | |||
| 1 | + | from tkinter import * | |
| 2 | + | import tkinter as tk | |
| 3 | + | import tkinter.messagebox | |
| 4 | + | import random | |
| 5 | + | class 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 | + | ||
| 40 | + | root = Tk() | |
| 41 | + | app = Window(root) | |
| 42 | + | root.wm_title("Number Guesser made with Tkinter") | |
| 43 | + | root.geometry("500x85") | |
| 44 | + | root.mainloop() | |
Novější
Starší