python3.3 using Tkinter to develop a guess number game example

Source: Internet
Author: User
Use this mini-game to learn how Tkinter is used 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
#修改缺陷: The user is correct, the prompt tag also prompts the message Edit by Hongten 2013-09-09
#即用户在答对了以后, prompting the label should not be followed by the user click the ' Guess ' button to change
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 Oh")
Else:
If val_a < nmaxn:
Nmaxn = val_a
num+=1
Label_tip_max.config (LABEL_TIP_MAX,TEXT=NMAXN)
Labelqval ("Big Oh")
Else:
Labelqval (' You've got the right answer ... ')

Def numguess ():
if num = = 1:
Labelqval (' I am! One answer! ')
Elif Num < 10:
Labelqval (' = = 10 times within the correct right ... Number of attempts: ' +str (num))
Elif Num < 50:
Labelqval (' OK, try Count: ' +str (num) ')
Else
Labelqval (' Well .... You've tried it 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= "than the size of the game")
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= "Off")
Btnclose.bind (" , Ebtnclose)
Btnclose.pack (side= "left")
Line_btn.pack (side = "Top")

Labelqval ("Please enter any integer between 0 and 1024:")
Entry_a.focus_set ()

Print (number)
Root.mainloop ()

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.