日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

PythonTkinter模塊

 昵稱61973759 2019-01-28

可以編寫出帶頁面的程序,不過由于界面不夠美化一般都是安裝程序中用到這些,僅僅對每個控件進行代碼演示。


Tkinter主體:

import tkinter

# 創(chuàng)建主窗口?

win = tkinter.Tk()

# 設(shè)置窗口標題?

win.title("sunck")

# 設(shè)置窗口大小與位置?

win.geometry("400x400+200+0")

# 進入消息循環(huán)

# ??程序運行起來?

win.mainloop()

label控件

import tkinter

win = tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

# win表示主窗體?

# text:文本內(nèi)容?

# bg:背景顏色?

# fg:字體顏色

# font:字體型號與大小?

# anchor:位置  N  NE  E  SE  S  SW  W  NW  CENTER?

# width:空間寬度?

# wraplength:指定text中文本多少寬度后開始換行

# justify:text中多行文本的對齊方式

l = tkinter.Label(win, text="sunckisagoodman", bg="pink", fg="red", font=("Arial",12), anchor=tkinter.SW, width=10, height=3, wraplength = 30, justify="right")

# 顯示控件?

l.pack()

win.mainloop()

button控件

import tkinter

def func():

     print("sunck is a good man")

win = tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

# 創(chuàng)建按鈕?

# 顯示窗口?

# 文本?

# 點擊按鈕響應的方法?

button=tkinter.Button(win, text="按鈕", width=10, height=10, command=func)

button.pack()

button1=tkinter.Button(win, text="按鈕1", command=lambda :print("sunck is a nice man"))

button1.pack()

win.mainloop()

entry控件

import tkinter

win = tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

# 綁定變量?

var = tkinter.Variable()

e = tkinter.Entry(win, textvariable=var, show="*")

e.pack()

# 設(shè)置文本框中的值?

var.set("sunck is a good man")

# 獲取文本框中的值?

print(var.get())

print(e.get())

win.mainloop()

 

5、點擊button將entry上的內(nèi)容打印出來

import tkinter

def func():

     print(ve.get())

win = tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

ve = tkinter.Variable()

e = tkinter.Entry(win, textvariable=ve)

e.pack()

button=tkinter.Button(win, text="顯示", command=func)

button.pack()

win.mainloop()

6、text控件

import tkinter

win = tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

#height:控制顯示行數(shù)

t = tkinter.Text(win, width=30, height=4)

t.pack()

# t.pack(side=tkinter.LEFT, fill=tkinter.Y) #滾動條寫法

info = """HAMLET: To be, or not to be--that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune

Or to take arms against a sea of troubles

And by opposing end them. To die, to sleep--

No more--and by a sleep to say we end

The heartache, and the thousand natural shocks

That flesh is heir to. 'Tis a consummation

Devoutly to be wished."""

#插入

t.insert(tkinter.INSERT, info)

win.mainloop()

7、帶滾動條的text

import tkinter

win = tkinter.Tk()

win.title("sunck")

# win.geometry("400x400+200+0")

s = tkinter.Scrollbar()

s.pack(side=tkinter.RIGHT, fill=tkinter.Y)

t = tkinter.Text(win, width=30, height=4)

t.pack(side=tkinter.LEFT, fill=tkinter.Y)

s.config(command=t.yview)

t.config(yscrollcommand=s.set)

info = """HAMLET: To be, or not to be--that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune

Or to take arms against a sea of troubles

And by opposing end them. To die, to sleep--

No more--and by a sleep to say we end

The heartache, and the thousand natural shocks

That flesh is heir to. 'Tis a consummation

Devoutly to be wished."""

t.insert(tkinter.INSERT, info)

win.mainloop()

8、CheckButton多選框控件

from tkinter import *

def updata():

    message = ""

    if hobby1.get() == 1:

        message += "Your Hobby is 1\n"

    if hobby2.get() == 1:

        message += "Your Hobby is 2\n"

    if hobby3.get() == 1:

        message += "Your Hobby is 3\n"

    txt.delete(0.0, END)#全部刪除txt內(nèi)容

    txt.insert(0.0, message)

win = Tk()

win.title("sunck")

label=Label(win,text = "Please check")

label.pack()

hobby1 = BooleanVar()

checkb1 = Checkbutton(win,text = "hobby1",variable = hobby1,command = updata)

checkb1.pack()

hobby2 = BooleanVar()

checkb2 = Checkbutton(win,text = "hobby2",variable = hobby2,command = updata)

checkb2.pack()

 

hobby3 = BooleanVar()

checkb3 = Checkbutton(win,text = "hobby3",variable = hobby3,command = updata)

checkb3.pack()

txt = Text(win,width = 50,height = 5,wrap = WORD)

txt.pack()

win.mainloop()

9、RadioButton單選框控件

from tkinter import *

def updata():

    print(v.get())

win = Tk()

win.title("sunck")

win.geometry("400x400+200+0")

v = IntVar()

Radiobutton(win, text="One", variable=v, value=1, command=updata).pack()

Radiobutton(win, text="Two", variable=v, value=2, command=updata).pack()

mainloop() 

10、Listbox控件基本使用

from tkinter import *

win = Tk()

win.title("sunck")

# win.geometry("400x400+200+0")

# 1、創(chuàng)建一個Listbox,向其中添加三個item

# lb = Listbox(win, selectmode = BROWSE)

# for item in ['good', 'nice', 'handsom', "grace", "noble", "sense"]:

#     #按順序向后添加

#     lb.insert(END, item)

# lb.pack()

# 在開始添加

# lb.insert(ACTIVE, "cool")

# 只添加一項將[]作為一個item

# lb.insert(END,["1","2","3"])

# 刪除  第一個為開始的索引值;第二個為結(jié)束的索引值,如果不指定則只刪除第一個索引項

# lb.delete(1,3)

# lb.delete(1)

# 選中函數(shù),selection_set函數(shù)有兩個參數(shù)第一個為開始的索引;第二個為結(jié)束的索引,如果不指定則只選中第一個參數(shù)指定的索引項

# lb.select_set(2, 4)

# 取消選中

# lb.select_clear(2,4)

# 得到當前Listbox中的item個數(shù)

# print(lb.size())

# 返回指定索引的項,參數(shù)第一個為開始的索引;第二個為結(jié)束的索引,如果不指定則只選中第一個參數(shù)指定的索引項

# print(lb.get(1, 3))

# print(lb.get(1))

# 返回當前返回的項的索引,不是item的值

# print(lb.curselection())

# 判斷 一個項是否被選中,使用索引

# print(lb.selection_includes(2))

# print(lb.selection_includes(5))

# 2、與BROWSE相似 的為SINGLE,但不支持鼠標移動選中位置

# v = StringVar()

# lb = Listbox(win, selectmode = SINGLE, listvariable=v)

# for item in ['good', 'nice', 'handsom']:

#     lb.insert(END, item)

# lb.pack()

# 打印當前列表中的項值

# print(v.get())

# 改變v的值,使用tuple可以與item對應

# v.set(('1000','200'))

# 事件綁定

# def printList(event):

#     print(lb.get(lb.curselection()))

# lb.bind('<Double-Button-1>',printList)

# 3、使用selectmode  = EXPANDED使用Listbox來支持Shift和Control

# lb = Listbox(win, selectmode = EXTENDED)

# for item in ['good', 'nice', 'handsom', "grace", "noble", "sense", 1, 2, 3, 4, 5, 6]:

#     lb.insert(END, item)

# lb.pack()

# # 運行程序,點中“python",shift + 點擊"widget",會選中所有的item

# # 運行程序,點中"python",control + 點擊"widget",會選中python和widget,第二項tkinter處于非選中狀態(tài)

# # 滾動條控件

# # 當內(nèi)容超過可視化區(qū)域時使用,如列表框

# scrl = Scrollbar(win)

# scrl.pack(side=RIGHT,fill=Y)

# # 指定Listbox的yscrollbar的回調(diào)函數(shù)為Scrollbar的set,表示滾動條在窗口變化時實時更新

# lb.configure(yscrollcommand=scrl.set)

# lb.pack(side=LEFT,fill=BOTH)

# # # 指定Scrollbar的command的回調(diào)函數(shù)是Listbar的yview

# scrl['command'] = lb.yview

# 4、創(chuàng)建一個可以多選的Listbox,使用屬性selectmaod

lb = Listbox(win, selectmode = MULTIPLE, )

for item in ['good', 'nice', 'handsom']:

    lb.insert(END, item)

lb.pack()

win.mainloop()

11、Scale控件

# Tkinter 中的 Scale 控件是一種可供用戶通過拖動指示器改變變量值的控件. 這種控件可以水平放置, 也可以豎直放置

from tkinter import *

win = Tk()

win.title("sunck")

win.geometry("400x400+200+0")

# 豎

# from起始值

# to結(jié)束值

# tickinterval選擇值將會變?yōu)樵摂?shù)值的倍數(shù)

# length 豎直時為高度,水平是為長度

scale1 = Scale(win, from_=0, to=40, tickinterval=8, length=300)

scale1.pack()

#默認值為起始值,可以通過set賦值

scale1.set(20)

# 獲取值

# def show():

#     print(scale1.get())

# Button(win, text="show", command=show).pack()

#橫 orient=HORIZONTAL

# scale2 = Scale(win, from_=0, to=200, orient=HORIZONTAL)

# scale2.pack()

win.mainloop()

12、Spinbox控件

from tkinter import *

win = Tk()

win.title("sunck")

win.geometry("400x400+200+0")

def change():

    print(v.get())

v = StringVar()

# from_:最小值

# to:最大值

# increment:步長

# values:每次更新值將使用values指定的值,不要和from_與to同時使用values=(0,2,4,6,8)

sp = Spinbox(win, from_=0, to=100, increment=1, textvariable=v, command=change)

sp.pack()

#設(shè)置值

v.set(20)

#取值

print(v.get())

win.mainloop()

13、Menu頂層菜單

from tkinter import *

def callback():

    print("sunck is a good man")

win = Tk()

win.title("sunck")

win.geometry("400x400+200+0")

menubar = Menu(win)

win.config(menu=menubar)

 

filemenu = Menu(menubar, tearoff=False)

for item in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript', "退出"]:

    if item == "退出":

        filemenu.add_separator()

        filemenu.add_command(label=item, command=win.quit)

    else:

        filemenu.add_command(label=item, command=callback)

filemenu2 = Menu(menubar, tearoff=False)

#accelerator快捷鍵

filemenu2.add_command(label="顏色設(shè)置", command=callback, accelerator='Ctrl+N')

filemenu2.add_command(label="字體設(shè)置")

menubar.add_cascade(label="語言", menu=filemenu)

menubar.add_cascade(label="設(shè)置", menu=filemenu2)

win.mainloop()

14、Menu右鍵菜單

from tkinter import *

win = Tk()

win.title("sunck")

win.geometry("400x400+200+0")

menubar = Menu(win)

def callback():

    print('sunck is a good man')

filemenu = Menu(menubar, tearoff=0)

for k in ['Python', 'PHP', 'CPP', 'C', 'Java', 'JavaScript', 'VBScript']:

    filemenu.add_command(label=k, command=callback)

    filemenu.add_separator()

menubar.add_cascade(label='語言', menu=filemenu)

def popup(event):

    menubar.post(event.x_root, event.y_root)

win.bind('<Button-3>', popup)

win.mainloop()

15、Frame控件

import tkinter

win = tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

tkinter.Label(win, text="frame", bg="red", font=("Arial",15)).pack()

frm = tkinter.Frame(win)

frm.pack()

# #left

frm_L = tkinter.Frame(frm)

tkinter.Label(frm_L, text="左上", bg="pink", font=("Arial",12)).pack(side=tkinter.TOP)

tkinter.Label(frm_L, text="左下", bg="green", font=("Arial",12)).pack(side=tkinter.TOP)

frm_L.pack(side=tkinter.LEFT)

#right

frm_R = tkinter.Frame(frm)

tkinter.Label(frm_R, text="右上", bg="yellow", font=("Arial",12)).pack(side=tkinter.TOP)

tkinter.Label(frm_R, text="右下", bg="purple", font=("Arial",12)).pack(side=tkinter.TOP)

frm_R.pack(side=tkinter.RIGHT)

win.mainloop()

16、Combobox下拉控件

import tkinter

from tkinter import ttk

def func(*args):

    # 當前選中的值

    print(comboxlist.get())

win = tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

var = tkinter.StringVar()

comboxlist = ttk.Combobox(win, textvariable = var)

# 設(shè)置下拉數(shù)據(jù)

comboxlist["value"] = ("1","2","3","4","5")

# 默認現(xiàn)實的值的下標

comboxlist.current(0)

# 綁定事件

comboxlist.bind("<<ComboboxSelected>>", func)

comboxlist.pack()

win.mainloop()

17、表格數(shù)據(jù)

import tkinter

from  tkinter import ttk

win=tkinter.Tk()

win.title("sunck")

win.geometry("800x400+200+0")

#表格

tree=ttk.Treeview(win)

tree.pack()

#表示列,不顯示

tree["columns"]=("name","age","height")

tree.column("name",width=100)

tree.column("age",width=100)

tree.column("height",width=100)

#顯示表頭

tree.heading("name",text="姓名")

tree.heading("age",text="年齡")

tree.heading("height",text="身高")

# #插入行

tree.insert("",0,text="line1" ,values=("1","2","3"))

tree.insert("",1,text="line2" ,values=("1","2","3"))

tree.insert("",2,text="line3" ,values=("1","2","3"))

tree.insert("",3,text="line4" ,values=("1","2","3"))

win.mainloop()

18、樹狀數(shù)據(jù)

import tkinter

from tkinter import ttk

win=tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

tree=ttk.Treeview(win)

tree.pack()

#一級樹枝

treeF1=tree.insert("",0,"中國",text="中國CHI",values=("F1"))

treeF2=tree.insert("",1,"美國",text="美國USA",values=("F2"))

treeF3=tree.insert("",2,"英國",text="英國ENG",values=("F3"))

#二級樹枝

threeF1_1=tree.insert(treeF1,0,"黑龍江",text="中國黑龍江",values=("F1_1"))

threeF1_2=tree.insert(treeF1,1,"吉林",text="中國吉林",values=("F1_2"))

threeF1_3=tree.insert(treeF1,2,"遼寧",text="中國遼寧",values=("F1_3"))

threeF1_4=tree.insert(treeF1,3,"北京",text="中國北京",values=("F1_4"))

threeF2_1=tree.insert(treeF2,0,"黑龍江1",text="美國黑龍江",values=("F2_1"))

threeF2_2=tree.insert(treeF2,1,"吉林1",text="美國吉林",values=("F2_2"))

threeF2_3=tree.insert(treeF2,2,"遼寧1",text="美國遼寧",values=("F2_3"))

threeF2_4=tree.insert(treeF2,3,"北京1",text="美國北京",values=("F2_4"))

threeF3_1=tree.insert(treeF3,0,"黑龍江2",text="英國黑龍江",values=("F3_1"))

threeF3_2=tree.insert(treeF3,1,"吉林2",text="英國吉林",values=("F3_2"))

threeF3_3=tree.insert(treeF3,2,"遼寧2",text="英國遼寧",values=("F3_3"))

threeF3_4=tree.insert(treeF3,3,"北京2",text="英國北京",values=("F3_4"))

#三級樹枝

threeF1_1_1=tree.insert(threeF1_1,0,"哈爾濱",text="黑龍江哈爾濱",values=("F1_1_1_1"))

threeF1_1_2=tree.insert(threeF1_1,1,"五常",text="黑龍江五常",values=("F1_1_1_2"))

threeF1_1_3=tree.insert(threeF1_1,2,"山河",text="黑龍江山河",values=("F1_1_1_3"))

win.mainloop()

19、絕對布局

import tkinter

win=tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

label1=tkinter.Label(win,text="good",bg="blue")

label2=tkinter.Label(win,text="nice",bg="yellow")

label3=tkinter.Label(win,text="handsome",bg="red")

# 絕對布局,窗口變化不影響

label1.place(x=10, y=10)

label2.place(x=50, y=50)

label3.place(x=100, y=100)

# label1.place(x=10,y=10,anchor=tkinter.NW)

# #輸入N,距離N10,W靠近,輸入W,距離W10 ,N靠近

# #輸入NW,N,W都距離10

# label2.place(x=10,y=10,anchor=tkinter.NW)

# label3.place(x=10,y=300,anchor=tkinter.W)

win.mainloop()

20、相對布局

import tkinter

win=tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

label1=tkinter.Label(win,text="good",bg="blue")

label2=tkinter.Label(win,text="nice",bg="yellow")

label3=tkinter.Label(win,text="handsome",bg="red")

# fill窗口變化Y方向同步變化  X  Y  BOTH

label1.pack(fill=tkinter.Y, side=tkinter.LEFT)

# side:貼邊  TOP  BOTTOM  CENTER   LEFT  RIGHT

label2.pack(fill=tkinter.X, side=tkinter.TOP)

label3.pack()

win.mainloop()

21、表格布局

import tkinter

win=tkinter.Tk()

win.title("sunck")

win.geometry("400x400+200+0")

label1=tkinter.Label(win,text="good",bg="blue")

label2=tkinter.Label(win,text="nice",bg="yellow")

label3=tkinter.Label(win,text="handsom",bg="red")

label4=tkinter.Label(win,text="nobo",bg="green")

#表格

label1.grid(row=0,column=0)

label2.grid(row=0,column=1)

label3.grid(row=1,column=0)

label4.grid(row=1,column=1)

win.mainloop()

22、鼠標點擊事件

from tkinter import *

root = Tk()

def printCoords(event):

    print(event.x,event.y)

bt1 = Button(root,text = 'leftmost button')

bt1.bind('<Button-1>',printCoords)

bt2 = Button(root,text = 'middle button')

bt2.bind('<Button-2>',printCoords)

bt3 = Button(root,text = 'rightmost button')

bt3.bind('<Button-3>',printCoords)

bt4 = Button(root,text = 'double click')

bt4.bind('<Double-Button-1>',printCoords)

bt5 = Button(root, text = 'triple click')

bt5.bind('<Triple-Button-1>',printCoords)

bt1.pack()

bt2.pack()

bt3.pack()

bt4.pack()

bt5.pack()

root.mainloop()

23、鼠標移動事件

#測試鼠標的移動事件

#<Bx-Motion>鼠標移動事件,x=[1,2,3]分別表示左中右鼠標操作

from tkinter import *

 

win = Tk()

def printCoords(event):

    print(event.x,event.y)

bt1 = Button(win,text = 'leftmost button')

bt1.bind('<B1-Motion>',printCoords)

bt2 = Button(win,text = 'middle button')

bt2.bind('<B2-Motion>',printCoords)

bt3 = Button(win,text = 'rightmost button')

bt3.bind('<B3-Motion>',printCoords)

bt1.grid()

bt2.grid()

bt3.grid()

win.bind('<B1-Motion>',printCoords)

win.mainloop()

24、鼠標釋放事件

from tkinter import *

root = Tk()

def printCoords(event):

    print(event.x,event.y)

bt1 = Button(root,text = 'leftmost button')

bt1.bind('<ButtonRelease-1>',printCoords)

bt2 = Button(root,text = 'middle button')

bt2.bind('<ButtonRelease-2>',printCoords)

bt3 = Button(root,text = 'rightmost button')

bt3.bind('<ButtonRelease-3>',printCoords)

bt1.grid()

bt2.grid()

bt3.grid()

root.mainloop()

25、進入事件

from tkinter import *

root = Tk()

def printCoords(event):

    print(event.x,event.y)

bt1 = Button(root,text = 'leftmost button')

bt1.bind('<Enter>',printCoords)

bt1.grid()

root.mainloop()

26、離開事件

from tkinter import *

root = Tk()

def printCoords(event):

    print(event.x,event.y)

bt1 = Button(root,text = 'leftmost button')

bt1.bind('<Leave>',printCoords)

bt1.grid()

root.mainloop()

27、響應特殊按鍵事件

from tkinter import *

root = Tk()

def printCoords(event):

    print('event.char = ',event.char)

    print('event.keycode = ',event.keycode)

bt1 = Button(root,text = 'Press BackSpace')

bt1.bind('<BackSpace>',printCoords)

bt2 = Button(root,text = 'Press Enter')

bt2.bind('<Return>',printCoords)

bt3 = Button(root,text = 'F5')

bt3.bind('<F5>',printCoords)

bt4 = Button(root,text = 'Left Shift')

bt4.bind('<Shift_L>',printCoords)

bt5 = Button(root,text = 'Right Shift')

bt5.bind('<Shift_R>',printCoords)

bt1.focus_set()

bt1.grid()

bt2.grid()

bt3.grid()

bt4.grid()

bt5.grid()

root.mainloop()

28、響應所有時間按鍵事件

from tkinter import *

root = Tk()

def printCoords(event):

    print('event.char = ',event.char)

    print('event.keycode = ',event.keycode)

bt1 = Button(root,text = 'Press BackSpace')

bt1.bind('<Key>',printCoords)

bt1.focus_set()

bt1.grid()

root.mainloop()

29、指定按鍵事件

from tkinter import *

root = Tk()

def printCoords(event):

    print('event.char = ',event.char)

    print('event.keycode = ',event.keycode)

bt1 = Button(root,text = 'Press BackSpace')

bt1.bind('a',printCoords)

bt1.focus_set()

bt1.grid()

root.mainloop()

30、組合按鍵事件

from tkinter import *

root = Tk()

def printCoords(event):

    print('event.char = ',event.char)

    print('event.keycode = ',event.keycode)

bt1 = Button(root,text = 'Press Shift - Up')

bt1.bind('<Shift-Up>',printCoords)

 

bt2 = Button(root,text = 'Control-Alt-a')

bt2.bind('<Control-Alt-a>',printCoords)

bt3 = Button(root,text = 'Control-Alt')

bt3.bind('<Control-Alt>',printCoords)

bt1.focus_set()

bt1.grid()

bt2.grid()

root.mainloop()

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多