大家好,我是天空之城,今天給大家帶來,python的GUI編程初探,這部分內容真的非常有意思
#感謝博友離愁無語說,海天一樹X
import tkinter as tk
from tkinter import * #導入tkinter庫
# top=tkinter.Tk()#創建窗口
#使窗口停下來,不一閃而去
# top.mainloop()
#--------------------------------------
'''
root = tkinter.Tk() #創建窗口
computerLanguages = ['C', 'C++', 'Python', 'Java']
humanLanguages = ['Chinese', 'English', 'Spanish']
listbox1 = Listbox(root) #創建第一個標籤盒子
listbox2 = Listbox(root)#創建第二個標籤盒子
for item in computerLanguages:#從第一個列表中取出所有元素放入標籤盒子
listbox1.insert(0, item)
for item in humanLanguages:#從第二個列表中取出所有元素放入標籤盒子
listbox2.insert(0, item)
listbox1.pack()#將小部件放入主窗口中
listbox2.pack()
root.mainloop()#進入消息循環
'''
'''mainloop就是進入到事件(消息)循環。一旦檢測到事件,就刷新組件。
譬如你輸入一個字符,就要立即在光標那個位置顯示出來(前提是你選中了文本框,也就是鼠標在文本框這個圖案的範圍內單擊過)。
又譬如你點擊了瀏覽器的首頁按鈕,那麼就要清除你瀏覽器裏的全部部件,然後重新繪製主頁的佈局和內容。
'''
#------------------------------------
#窗口添加按鈕,設置按鈕函數,一旦點擊按鈕就打印'hello button'
def clickButton():
print('hello button')
root = Tk()
root.title('magic_box')
root.geometry('500x250+100+100')
Button(root, text='MyButton', command=clickButton).pack()
lablevar = tk.StringVar()
lablevar.set('開始學習python')
lable = tk.Label(root,textvariable=lablevar,bg='red',font=('宋體', 25),width=15, height=3)
#textvariable=lablevar, # 標籤的文字
lable.pack()
#使窗口停下來,不一閃而去
root.mainloop()
#-----------------------------------------------
6.線程小遊戲,來挑選你的夢中女神吧。。
# 1.有12個備選選項和2個功能按鈕 確定備選選項和功能按鈕的位置
# 2.點擊開始會不斷旋轉。選中的時候背景顏色爲紅色,點擊停止結束
import threading
import tkinter
import time
# 1.實現窗口
root = tkinter.Tk()
root.title('美女聯盟')
root.minsize(300,300)
# 2 擺放按鈕
btn1 = tkinter.Button(root, text='蒼老師', bg='red')
btn1.place(x=20, y=20, width=50, height=50)
btn2 = tkinter.Button(root, text='趙雅芝', bg='white')
btn2.place(x=90, y=20, width=50, height=50)
btn3 = tkinter.Button(root, text='李若彤', bg='white')
btn3.place(x=160, y=20, width=50, height=50)
btn4 = tkinter.Button(root, text='范冰冰', bg='white')
btn4.place(x=230, y=20, width=50, height=50)
btn5 = tkinter.Button(root, text='趙薇', bg='white')
btn5.place(x=230, y=90, width=50, height=50)
btn6 = tkinter.Button(root, text='林青霞', bg='white')
btn6.place(x=230, y=160, width=50, height=50)
btn7 = tkinter.Button(root, text='朱茵', bg='white')
btn7.place(x=230, y=230, width=50, height=50)
btn8 = tkinter.Button(root, text='張曼玉', bg='white')
btn8.place(x=160, y=230, width=50, height=50)
btn9 = tkinter.Button(root, text='劉巖', bg='white')
btn9.place(x=90, y=230, width=50, height=50)
btn10 = tkinter.Button(root, text='劉詩詩', bg='white')
btn10.place(x=20, y=230, width=50, height=50)
btn11 = tkinter.Button(root, text='高圓圓', bg='white')
btn11.place(x=20, y=160, width=50, height=50)
btn12 = tkinter.Button(root, text='鄧紫棋', bg='white')
btn12.place(x=20, y=90, width=50, height=50)
#2.1將所有的選項放到列表中 目的:爲了操作這些選項
hero_list = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12]
# 定義一個標記
stop_sign = False # 默認是停止
stop_id = None
# 3 定義一個函數 1.循環備選選項 2.設置選項顏色
def round():
global stop_id
i = 1
if isinstance(stop_id,int):
i = stop_id
while True:
time.sleep(0.3)
for x in hero_list:
x['bg'] = 'white'
hero_list[i]['bg'] = 'red'
i += 1
print('當前的i爲:',i)
if i >= len(hero_list):
i = 0
if stop_sign == True:
stop_id = i
break
# 4 定義停止的方法
def stop():
global stop_sign
if stop_sign == True:
return
stop_sign = True
# 5 定義開始的方法
def newtask():
global stop_sign
stop_sign = False
t = threading.Thread(target=round)
t.start()
# 6.設置按鈕
btn_start = tkinter.Button(root,text='開始',command=newtask)
btn_start.place(x=90,y=125,width=50,height=50)