码迷,mamicode.com
首页 > 编程语言 > 详细

python学习之GUI

时间:2016-10-14 00:59:24      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:学习   python   gui   

button按钮的生成:

import Tkinter as tk
window=tk.Tk()

def buttonClick():
    print "Beep!"

button=tk.Button(window,text="Click me!",command=buttonClick)
button.pack()
window.mainloop()


同理将方法改为改变按钮上的文字

import Tkinter as tk
window=tk.Tk()

def buttonClick():
#    print "Beep!"
    button.config(text="Clicked!")
button=tk.Button(window,text="Click me!",command=buttonClick)
button.pack()
window.mainloop()


点击按钮的计数

import Tkinter as tk
window=tk.Tk()

count=0

def buttonCount():
    global count
    count+=1
    button.config(text=str(count))

button=tk.Button(window,text="count",command=buttonCount)
button.pack()
window.mainloop()

创建文本框,里面有一个按钮和文本框,通过获取文本框的内容,每点击一次按钮,都复制文本框中的内容并一起显示在文本框中。

import Tkinter as tk
window=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()

其中,entry.insert中参数第一个为插入的位置。


将文本框中的内容反面显示于文本框中

import Tkinter as tk
window=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()

添加密码框,用于输入密码的文本框常常在输入密码时不会显示密码,而是在每个字符的位置显示星号(*),使用方式:在添加文本框时,添加一个额外的具名自变量,这样文本框就变成了一个密码框

在GUI中会有一个输入密码的框和一个提交密码的按钮。如果密码正确,底部会出现一个写有Correct的标签,负责它会写Incorrect。

import Tkinter as tk
window=tk.Tk()

def checkPassword():
    password="Apple"
    enterPass=passwordEntry.get()
    if enterPass == password:
        confirmLabel.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()

构造句子生成器:列表与随机函数的配合使用

import Tkinter as tk
import random
window=tk.Tk()

def randomNoun():
    nouns=["cats","hippos","cakes"]
    noun=random.choice(nouns)
    return noun

def randomVerb():
    verbs=["eats","likes","hates","has"]
    verb=random.choice(verbs)
    return verb

def  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()

猜数字游戏:生成一个1-10之间的随机数,看是否成功猜测,并计算游戏的胜利次数(标签显示)

import random
import Tkinter as tk
window=tk.Tk()

maxNo=10
score=0
rounds=0

def 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 Tkinter as tk
window=tk.Tk()

slider=tk.Scale(window,from_=0,to=100)
slider.pack()
tk.mainloop()

滚动条的创建与使用

import Tkinter as tk
window=tk.Tk()

slider=tk.Scale(window,from_=0,to=100)
slider.pack()
tk.mainloop()

画布的创建

import Tkinter as tk
window=tk.Tk()

colour="#FF0000"
canvas=tk.Canvas(window,height=300,width=300,bg=colour)
canvas.pack()
window.mainloop()

颜色选择器:画布与滚动条的配合使用

注:通过三个滚动条的选择从而决定三原色红,绿,蓝三个颜色的占用,从而组成喜欢的颜色。而在实际画画中,所有的颜色由红,黄,蓝混合组成。

import Tkinter as tk
window=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()

改进:添加一个文本框用来显示三原色的选择(16进制)

import Tkinter as tk
window=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()

一个计点击速度的游戏:记录点击的次数,直到达到目地数,输出所花费时间

import Tkinter as tk
import time
window=tk.Tk()

clicks=0
start=0
goal=10

def 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()


本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1861426

python学习之GUI

标签:学习   python   gui   

原文地址:http://10541556.blog.51cto.com/10531556/1861426

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!