This article mainly introduces python3.3 using tkinter to develop a guess digital game example. If you need a friend, you can refer to this small game to learn how to use tkinter in ython3.3.
The Code is as follows:
#-*-Coding: UTF-8 -*-
Import tkinter as tk
Import sys
Import random
Import re
Number = random. randint (0,1024)
Running = True
Num = 0
Nmaxn= 1024
Nminn = 0
Def eBtnClose (event ):
Root. destroy ()
Def eBtnGuess (event ):
Global nmaxn
Global nminn
Global num
Global running
# Modify the defect: the user is correct, and the prompt "Edit by Hongten 2013-09-09" is displayed.
# That is, after the user is correct, the prompt tag should not change as the user clicks the "Guess" button.
If running:
Val_a = int (entry_a.get ())
If val_a = number:
Labelqval ("Congratulations! ")
Num + = 1
Running = False
NumGuess ()
Elif val_a <number:
If val_a> nminn:
Nminn = val_a
Num + = 1
Label_tip_min.config (label_tip_min, text = nminn)
Labelqval ("small ")
Else:
If val_a <nmaxn:
Nmaxn = val_a
Num + = 1
Label_tip_max.config (label_tip_max, text = nmaxn)
Labelqval ("big ")
Else:
Labelqval ('you are correct ...')
Def numGuess ():
If num = 1:
Labelqval ('depend on me! One answer! ')
Elif num <10:
Labelqval ('= within 10 times, the correct answer is awesome... Number of attempts: '+ str (num ))
Elif num <50:
Labelqval ('Okay. Number of attempts: '+ str (num ))
Else:
Labelqval ('Okay ..... You have tried more than 50 times .... Number of attempts: '+ str (num ))
Def labelqval (vText ):
Label_val_q.config (label_val_q, text = vText)
Root = tk. Tk (className = "game by size ")
Root. geometry ("400x90 + 200 + 200 ")
Line_a_tip = tk. Frame (root)
Label_tip_max = tk. Label (line_a_tip, text = nmaxn)
Label_tip_min = tk. Label (line_a_tip, text = nminn)
Label_tip_max.pack (side = "top", fill = "x ")
Label_tip_min.pack (side = "bottom", fill = "x ")
Line_a_tip.pack (side = "left", fill = "y ")
Line_question = tk. Frame (root)
Label_val_q = tk. Label (line_question, width = "80 ")
Label_val_q.pack (side = "left ")
Line_question.pack (side = "top", fill = "x ")
Line_input = tk. Frame (root)
Entry_a = tk. Entry (line_input, width = "40 ")
BtnGuess = tk. Button (line_input, text = "Guess ")
Entry_a.pack (side = "left ")
Entry_a.bind (' ', EBtnGuess)
BtnGuess. bind (' ', EBtnGuess)
BtnGuess. pack (side = "left ")
Line_input.pack (side = "top", fill = "x ")
Line_btn = tk. Frame (root)
BtnClose = tk. Button (line_btn, text = "close ")
BtnClose. bind (' ', EBtnClose)
BtnClose. pack (side = "left ")
Line_btn.pack (side = "top ")
Labelqval ("enter any integer between 0 and 1024 :")
Entry_a.focus_set ()
Print (number)
Root. mainloop ()