python3.3 uses Tkinter to develop a guessing number game example _python

Source: Internet
Author: User

Use this little game to learn how to use the Tkinter in ython3.3

Copy Code code 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
#修改缺陷: User is correct, prompt tag also prompts information Edit by Hongten 2013-09-09
#即用户在答对了以后, the hint label 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 ("Little 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 (' Holy crap! One answer! ')
Elif Num < 10:
Labelqval (' = = = 10 times within the right to the correct ... Number of attempts: ' +str (num)
Elif Num < 50:
Labelqval (' Oh, yes. Try again: ' +str (num) ')
Else
Labelqval (' OK ... 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= "Game Over 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 (' <Return> ', ebtnguess)
Btnguess.bind (' <Button-1> ', 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 (' <Button-1> ', ebtnclose)
Btnclose.pack (side= "left")
Line_btn.pack (side = "Top")

Labelqval ("Please enter any integer from 0 to 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.