python tkinter 基本操作與事件

來源:互聯網
上載者:User

標籤:ext   顏色   int   nbsp   字型大小   右鍵   文字   import   button   

基本操作

import tkinter as tk           # 引入tk 包win=tk.Tk()                    # 引入視窗對象win.title("視窗標題")          # 視窗標題win.geometry("200x100")        # 視窗寬 x  高win.geometry("+500+200")       # 視窗出入螢幕的位置 左 上lable=tk.Label(win,            # 父視窗對象    text=‘OMG! this is TK!‘,   # 標籤的文字    bg=‘green‘,                # 背景顏色    font=(‘Arial‘, 12),        # 字型和字型大小    width=15, height=2         # 標籤長寬    )
‘‘‘
label 或其他標籤設定樣式可以字串形式,也可以是數組形式
label[‘height‘]=5
label[‘width‘]=20
‘‘‘
#lable.pack() # 布局在合適的位置 這裡使用的是 pack 布局lable.grid() # 布局在合適的位置 這裡使用的是 grid 布局win.mainloop() #事件迴圈

 

操作事件(command)

# 事件操作import tkinter as tk# 點擊事件執行函數def p_label():    global root    label=tk.Label(root,text="python")    label.pack()root=tk.Tk()button=tk.Button(root,text=‘點擊按鈕‘,command=p_label) # command 點擊事件命令button.pack()root.mainloop()

 操作事件(bind)

import tkinter as tk def p_label(events): #必須跟一個參數    global root    label = tk.Label(root,text=‘我愛python‘)    label.pack() root = tk.Tk()button = tk.Button(root,text=‘點我‘)button.bind("<Button-1>",p_label)  # 滑鼠點擊事件 <Button-1>表示左鍵 2表示滾輪 3表示右鍵button.pack()root.mainloop()

 

python tkinter 基本操作與事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.