今天小編就爲大家分享一篇在python tkinter界面中添加按鈕的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
tkinter是python自帶的GUI庫,可以實現簡單的GUI交互,該例子添加了五種不同效果的Button,如圖:
from tkinter import *
from tkinter import messagebox #python3.0的messagebox,屬於tkinter的一個組件
top = Tk()
top.title("button test")
def callback():
messagebox.showinfo("Python command","人生苦短、我用Python")
Button(top, text="外觀裝飾邊界附近的標籤", width=19,bg="red",relief="raised").pack()
Button(top, text="設置按鈕狀態",width=21,state="disable").pack()
Button(top, text="設置bitmap放到按鈕左邊位置", compound="left",bitmap="error").pack()
Button(top, text="設置command事件調用命令", fg="blue",bd=2,width=28,command=callback).pack()
Button(top, text ="設置高度寬度以及文字顯示位置",anchor = 'sw',width = 30,height = 2).pack()
top.mainloop()
補充知識:Python筆記之Tkinter(Spinbox數值框帶加減按鈕)
一、目標
學習Tkinter製作窗體軟件的基礎,Spinbox,此功能可以做出比如遊戲裏的購物數量加減。
二、試驗平臺
windows7 , python3.7
三、直接上代碼
import tkinter
def xFunc():
print(xVariable.get())
win = tkinter.Tk()
win.title("Kahn Software v1") # #窗口標題
win.geometry("500x500+200+20")
'''
此功能可以做出比如遊戲裏的購物數量加減。
from_=0, 開始值爲0
to=100 結束值設定爲100
increment=10 設定步長爲10,默認爲1。
values=(0, 2, 4, 6, 8, 21, 37, 36) 可以設定值是固定的哪些,用了這玩意就不能用from_ to了
'''
xVariable = tkinter.StringVar() # #設定一個字符串類型的變量
# #創建scale滾動條
sb = tkinter.Spinbox(win, from_=0, to=100, increment=1, textvariable=xVariable, command=xFunc)
# sb = tkinter.Spinbox(win, values=(0, 2, 4, 6, 8, 21, 37, 36)) # #值寫死
sb.pack()
# xVariable.set(18) # #賦值
# result = xVariable.get(xVariable) # #取值
# print(result)
win.mainloop() # #窗口持久化
最後給大家推薦一個資源很全的python學習聚集地,[點擊進入],這裏有我收集以前學習心得,學習筆
記,還有一線企業的工作經驗,且給大定on零基礎到項目實戰的資料,大家也可以在下方,留言,把不
懂的提出來,大家一起學習進步