1. 程式人生 > >Python 簡單的Gui介面

Python 簡單的Gui介面

#coding:utf-8
import Tkinter as tk
import time
import random

window = tk.Tk()
window.title('my window')
window.geometry('300x200')

on_hit = 0
ss = ''
def hit_me():
    global on_hit
    global ss
    if on_hit == 0:
        on_hit+=1
        lis = ['鍵盤','ipad']
        num = random.randint(0,1)
        ss = lis[num]
        var.set(ss)
    else:
        var.set('就是%s了'%(ss))

var = tk.StringVar()
l2 = tk.Label(window,text='鍵盤還是ipad呢',bg='green',font=('Arial', 12),width=15, height=2)
l2.pack()
l = tk.Label(window,textvariable=var,bg='green',font=('Arial', 12),width=15, height=2)
l.pack(side='bottom')

b = tk.Button(window,text='點我',width=15,height=2,command=hit_me)
b.pack()

window.mainloop()