Python Learning GUI

Source: Internet
Author: User
Tags rounds

Button's Build:

Import Tkinter as TKWINDOW=TK. Tk () def buttonclick (): print "beep!" Button=tk. Button (window,text= "Click me!", Command=buttonclick) Button.pack () Window.mainloop ()


The same way change the method to the text on the button

Import Tkinter as TKWINDOW=TK.    Tk () def buttonclick (): # print "beep!" Button.config (text= "clicked!") Button=tk. Button (window,text= "Click me!", Command=buttonclick) Button.pack () Window.mainloop ()


Click the Count of buttons

Import Tkinter as TKWINDOW=TK. Tk () Count=0def buttoncount (): Global Count Count+=1 button.config (TEXT=STR (count)) button=tk. Button (window,text= "Count", Command=buttoncount) Button.pack () Window.mainloop ()

Create a text box with a button and text box that copies the contents of the text box and displays it together in a text box by getting the contents of the text box, each time you click the button.

Import Tkinter as TKWINDOW=TK. Tk () def changestr (): Stringcopy=entry.get () Entry.insert (0,stringcopy) entry=tk. Entry (window) button=tk. Button (window,text= "Strchange", Command=changestr) Entry.pack () Button.pack () Window.mainloop ()

Where the first parameter in the Entry.insert is the inserted position.


Display the contents of the text box in the text box

Import Tkinter as TKWINDOW=TK. Tk () def changestr (): Stringcopy=entry.get () stringcopy=stringcopy[::-1] Entry.delete (0,TK. END) Entry.insert (0,stringcopy) entry=tk. Entry (window) button=tk. Button (window,text= "Strchange", Command=changestr) Entry.pack () Button.pack () Window.mainloop ()

The Add Password box, the text box used to enter a password, often does not display a password when entering a password, but instead displays an asterisk (*) at the location of each character, using: When adding a text box, add an additional named argument so that the text box becomes a password box

In the GUI there will be a box to enter the password and a button to submit the password. If the password is correct, a correct tag will appear at the bottom, which will write incorrect.

Import Tkinter as TKWINDOW=TK. Tk () def checkpassword (): password= "Apple" Enterpass=passwordentry.get () if Enterpass = = Password:confirml Abel.config (text= "Correct") Else:confirmLabel.config (text= "incorrect") passwordlabel=tk. Label (window,text= "Password") passwordentry=tk. Entry (window,show= "*") button=tk. Button (window,text= "Enter", Command=checkpassword) confirmlabel=tk. Label (window) passwordlabel.pack () Passwordentry.pack () Button.pack () Confirmlabel.pack () Window.mainloop ()

Construct a sentence generator: list and random functions in conjunction with

IMPORT TKINTER AS TKIMPORT RANDOMWINDOW=TK. Tk () Def randomnoun ():     nouns=["Cats", "hippos", "cakes"]    noun= Random.choice (nouns)     return noundef randomverb ():     verbs=["eats", "likes", "hates", "has"]    verb=random.choice (verbs)      Return verbdef  buttonclick ():     name=nameentry.get ()      verb=randomverb ()     noun=randomnoun ()     sentence=name+ " " +verb+ " " +noun    result.delete (0,TK. END)     result.insert (0,sentence) namelabel=tk. Label (window,text= "Name:") nameentry=tk. Entry (window) button=tk. Button (window,text= "Generate", Command=buttonclick) result=tk. Entry (window) namelabel.pack () Nameentry.pack () Button.pack () Result.pack () Window.mainloop () 

Guess number game: Generate a random number between 1-10 to see if it's a successful guess and count the number of wins (label display)

Import randomimport tkinter as tkwindow=tk. Tk () Maxno=10score=0rounds=0def buttonclick ():    global score     global rounds    try:        guess=int ( Guessbox.get ())         if 0<guess<=maxNo:             result=random.randrange (1,maxNo+1)              if guess==result:                 score=score+1             rounds+=1        else:             result= "Entry not valid"      except:        result= "Entry not valid"     resultlabel.config (text=result)      scorelabel.config (TEXT=STR (Score) + "/" +str (rounds))     guessbox.delete (0,TK. END) guesslabel=tk. Label (window,text= "Enter a number from 1 to" +str (maxno)) guessbox=tk. Entry (window) resultlabel=tk. Label (window) scorelabel=tk. Label (window) button=tk. button (window,text= "Guess", Command=buttonclick) Guesslabel.pack () Guessbox.pack () Resultlabel.pack () Scorelabel.pack () Button.pack () Window.mainloop () "IMPORT&NBSP;TKINTER&NBSP;AS&NBSP;TKWINDOW=TK". Tk () slider=tk. Scale (WINDOW,FROM_=0,TO=100) Slider.pack () Tk.mainloop ()

Creation and use of scroll bars

Import Tkinter as TKWINDOW=TK. Tk () slider=tk. Scale (WINDOW,FROM_=0,TO=100) Slider.pack () Tk.mainloop ()

Creating a canvas

Import Tkinter as TKWINDOW=TK. Tk () colour= "#FF0000" CANVAS=TK. Canvas (Window,height=300,width=300,bg=colour) Canvas.pack () Window.mainloop ()

Color Picker: Working with the canvas with the scroll bar

Note: Through the selection of three scroll bars to determine the three primary colors red, green, blue three-color occupy, thus forming a favorite color. In the actual painting, all the colors are composed of red, yellow, and blue blends.

Import Tkinter as TKWINDOW=TK. Tk () def sliderupdate (source): Red=redslider.get () Green=greenslider.get () blue=blueslider.get () colour= "#% 02x%02x%02x "% (red,green,blue) canvas.config (bg=colour) redslider=tk. Scale (Window,from_=0,to=255,command=sliderupdate) greenslider=tk. Scale (Window,from_=0,to=255,command=sliderupdate) blueslider=tk. Scale (Window,from_=0,to=255,command=sliderupdate) canvas=tk. Canvas (window,width=200,height=200) Redslider.grid (row=1,column=1) Greenslider.grid (row=1,column=2) Blueslider.grid (row=1,column=3) Canvas.grid (row=2,column=1,columnspan=3) Tk.mainloop ()

Improved: Add a text box to display the selection of three primary colors (16 binary)

Import Tkinter as TKWINDOW=TK. Tk () def sliderupdate (source): Red=redslider.get () Green=greenslider.get () blue=blueslider.get () colour= "#% 02x%02x%02x "% (red,green,blue) canvas.config (bg=colour) hextext.delete (0,TK. END) Hextext.insert (0,colour) redslider=tk. Scale (Window,from_=0,to=255,command=sliderupdate) greenslider=tk. Scale (Window,from_=0,to=255,command=sliderupdate) blueslider=tk. Scale (Window,from_=0,to=255,command=sliderupdate) canvas=tk. Canvas (window,width=200,height=200) hextext=tk. Entry (window) Redslider.grid (row=1,column=1) Greenslider.grid (row=1,column=2) Blueslider.grid (row=1,column=3) Canvas.grid (row=2,column=1,columnspan=3) Hextext.grid (row=3,column=1,columnspan=3) Tk.mainloop ()

A game with click Speed: Record the number of clicks until the target is reached and the output takes time

IMPORT&NBSP;TKINTER&NBSP;AS&NBSP;TKIMPORT&NBSP;TIMEWINDOW=TK. Tk () Clicks=0start=0goal=10def buttonclick ():    global clicks     global start    if clicks==0:         start=time.time ()         clicks=clicks+1     Elif clicks+1>=goal:        score=time.time ()-start         label.config (text= "time: " +str (score))          clicks=0    else:         Clicks=clicks+1    slider.set (clicks) button=tk. Button (window,text= "Click me", Command=buttonclick) slider=tk. Scale (Window,from_=0,to=goal) label=tk. Label (window) button.pack () Slider.pack () Label.pack () Window.mainloop () 


This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1861426

Python Learning GUI

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.