Python Tkinter 學習歷程 一


一丶一個簡單的程序

from tkinter import * #引入所有類
#查看tk版本
#tkinter._test()
root = Tk(); #對這個類進行實例化 w1 = Label(root,text="我是來搞笑的啦",background = "green" ) #組件 w2 = Label(root,text="我是來搞笑的啦",background = "red" ) w1.pack();#布局
w2.pack(side=TOP,expand = NO,fill=BOTH); #紅色位置應該在下面的,但是在pack設置了參數,就可以進行布局處理了
root.mainloop() #事件循環
#關於更多的pack參數,請看Python源碼注釋,pack不能同時和grid布局同時使用,...
"""Pack a widget in the parent widget. Use as options:
after=widget - pack it after you have packed widget
anchor=NSEW (or subset) - position widget according to
given direction
before=widget - pack it before you will pack widget
expand=bool - expand widget if parent size grows
fill=NONE or X or Y or BOTH - fill widget if widget grows
in=master - use master to contain this widget
in_=master - see 'in' option description
ipadx=amount - add internal padding in x direction
ipady=amount - add internal padding in y direction
padx=amount - add padding in x direction
pady=amount - add padding in y direction
side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
"""

效果:

 

二丶點擊事件綁定(1)

from tkinter import * #引入所有類
def xinlabel():
    global xin
    s = Label(xin,text ="I love python!")
    s.pack()

xin = Tk();
b1 = Button(xin,text="波波",command=xinlabel)
b1.pack()
xin.mainloop()

效果:

 

三丶界面簡單美化

from tkinter import * #引入所有類
xin = Tk();
b1 = Button(xin,text="波波")
b1['width'] = 20
b1['height'] = 4
b1.pack()
b2 = Button(xin,text="BOBO")
b2['width'] = 25
b2['background'] = 'red'
b2.pack()
xin.mainloop()
"""
1.關於按鈕,我們重點理解的就是它如何和事件進行綁定 的。 
2.當然一些其他屬性用來美化按鈕也很重要。
"""

效果:

 四 . grid布局

""""brid布局"""
from tkinter import *
xin  = Tk()
Label(xin,text = "賬號:").grid(row=0,sticky=W)
#Entry 輸入框,row 第幾行
#sticky默認的空間會在網格中居中顯示。你可以使用sticky選項去指定對齊方式,可以選擇的值有:N/S/E/W,分別代表上/下/左/右。如果你想讓label靠左顯示,你可以設置stricky的值為W
#column 相當於css的padding-left ^_^ 吧
Entry(xin).grid(row=0,column=1,sticky=E) Label(xin,text = "密碼:").grid(row=1,sticky=W) Entry(xin).grid(row=1,column=1,sticky=E) Button(xin,text=" 登錄 ").grid(row=2,column=1,sticky=E) xin.mainloop()

效果

五丶事件

事件是什么?, event 是事件的英文表述,事件常用的函數是bind  ,用法: 窗體對象.bind(事件類型,回調函數) 

from tkinter import *
def xinlabel (event):
    #global xin
    s = Label(xin,text="事件函數!")
    s.pack();

xin = Tk();
b1 = Button(xin,text="點我彈出文字1");
b2 = Button(xin,text="點我彈出文字2");
#特別理解好b1 = 那個Button,就綁定那個事件,也就是編程中的 對象 , 不是生活中的對象
b1.bind("<Button-1>",xinlabel)
b1.pack()
b2.pack()
xin.mainloop()
"""
bind
第一個參數是綁定<Button-1>按鈕,前面是事件對象,其中1,2,3 分別代表鼠標左鍵,中,右
第二個參數是函數名,不要帶任何標點符號,不然出錯
<KeyPress-A>表示 A 鍵被按下,其中的 A 可以換成其他 的鍵位。
<Control-V>表示按下的是 Ctrl 和 V 鍵,V 可以換成其他 鍵位。
<F1>表示按下的是 F1 鍵,對於 Fn 系列的,都可以隨便
"""

效果和二一樣的 

六丶登陸事件

""""登陸框"""
from tkinter import *
#定義一個注冊方法
def reg():
    s1 = e1.get() #獲取e1輸入框的內容
    s2 = e2.get()
    t1 = len(s1)
    t2 = len(s2)
    if s1 =='111' and s2 =='111':
        c['text'] =='登陸成功' #顯示內容
    else:
        c['text'] == "用戶名或密碼錯誤!"
        e1.delete(0,t1) #end參數可以清空內容 和t1 億元
        e2.delete(0,t2)
root = Tk() #實例化一個對象
l = Label(root,text = "用戶名:") #創建一個Label標簽
l.grid(row = 0,column = 0,sticky = W) #布局

e1 = Entry(root) #創建一個輸入框
e1.grid(row = 0, column = 1,sticky = E)

l2 = Label(root,text="密碼:")
l2.grid(row = 1 , column = 0,sticky = W )

e2 = Entry(root)
e2['show'] = '*' #密碼用 show = '*' 就可以隱藏了
e2.grid(row = 1, column = 1,sticky = E)

b = Button(root,text="登陸",command = reg ) #按鈕 等於reg 方法
b.grid(row = 2,column = 1,sticky = E)

#顯示登陸內容
c = Label(root,text='')
c.grid(row = 3)
root.mainloop()

效果同上登陸框

七丶菜單
"""菜單"""
from tkinter import *
root = Tk()
#創建一個一級菜單對象
menubar = Menu(root)
#創建一個一級子菜單
FileMenu = Menu(menubar, tearoff=False)  # tearoff=False 表示這個菜單不可以被拖拽出來
#新建一個子菜單列
FileMenu.add_command(label='打開')
FileMenu.add_command(label='導入')
#一個下拉菜單的分割線
FileMenu.add_separator()
FileMenu.add_command(label='退出')
#為一級創建多級菜單 , 參數2為 2 級菜單對象 參考第七行
menubar.add_cascade(label=' 文件 ', menu = FileMenu)
#root['menu'] = menubar #功能同下
root.config(menu=menubar)

mainloop()

效果:

八丶消息框

"""消息框"""
from tkinter.messagebox import *
#showinfo(title="你好",message="2017")
#$print("info", showinfo("Spam", "Egg Information"))
#print("warning", showwarning("Spam", "Egg Warning"))
#print("error", showerror("Spam", "Egg Alert"))
#print("question", askquestion("Spam", "Question?"))
#print("proceed", askokcancel("Spam", "Proceed?"))
#print("yes/no", askyesno("Spam", "Got it?"))
#print("yes/no/cancel", askyesnocancel("Spam", "Want it?"))
#print("try again", askretrycancel("Spam", "Try again?"))
t = askyesno("Spam", "Got it?")
if t :
    print("111")
else:
    print("222")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM