python tkinter chk
生活随笔
收集整理的這篇文章主要介紹了
python tkinter chk
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python tkinter chk 視頻過程中的練習(xí), 可以在python2.7下運(yùn)行.001: hello,world:1
2
3
4
5
6
from Tkinter import Label, Tkroot = Tk()
thelabel = Label(root, text="This is too easy")
thelabel.pack()
root.mainloop()002: Button pack布局1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from Tkinter import *root = Tk()topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)button1 = Button(topFrame, text="Button 1", fg="red")
button2 = Button(topFrame, text="Button 2", fg="blue")
button3 = Button(topFrame, text="Button 3", fg="green")
button4 = Button(bottomFrame, text="Button 4", fg="purple")button1.pack(side=LEFT)
button2.pack(side=LEFT)
button3.pack(side=LEFT)
button4.pack(side=BOTTOM)root.mainloop()003: Label和pack布局1
2
3
4
5
6
7
8
9
10
11
12
13
from Tkinter import *root = Tk()one = Label(root, text="one", bg="red", fg="white")
two = Label(root, text="two", bg="green", fg="black")
three = Label(root, text="three", bg="blue", fg="white")one.pack()
two.pack(fill=X)
three.pack(side=LEFT, fill=Y)root.mainloop()004: grid布局.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
from Tkinter import *root = Tk()label_1 = Label(root, text="Name")
label_2 = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root)label_1.grid(row=0)
label_2.grid(row=1)
entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1)root.mainloop()005: grid布局1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from Tkinter import *root = Tk()label_1 = Label(root, text="Name")
label_2 = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root)label_1.grid(row=0, sticky=E)
label_2.grid(row=1, sticky=E)
entry_1.grid(row=0, column=1)
entry_2.grid(row=1, column=1)c = Checkbutton(root, text="Keep me logged in")
c.grid(columnspan=2)root.mainloop()006: Button和事件1
2
3
4
5
6
7
8
9
10
11
from Tkinter import *root = Tk()def printName():print("Chello my name is Bucky!")button_1 = Button(root, text="Print my name", command=printName)
button_1.pack()root.mainloop()007: 綁定事件: 左鍵,中鍵,右鍵1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#coding:utf8
from Tkinter import *root = Tk()def printName(event):print("Chello my name is Bucky!")button_1 = Button(root, text="Print my name")
'''
<Button-1> 鼠標(biāo)左鍵
<Button-2> 鼠標(biāo)中鍵
<Button-3> 鼠標(biāo)右鍵
'''
button_1.bind("<Button-1>", printName)
button_1.pack()root.mainloop()008: 綁定事件: 左鍵,中鍵,右鍵1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from Tkinter import *root = Tk()def leftClick(event):print "left"def middleClick(event):print "middle"def rightClick(event):print "right"frame = Frame(root, width=300, height=250)
frame.bind("<Button-1>", leftClick)
frame.bind("<Button-2>", middleClick)
frame.bind("<Button-3>", rightClick)
frame.pack()root.mainloop()009: Python GUI with Tkinter-8-Using Classes1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-8-Using Classes
'''
from Tkinter import *class BuckysButtons:def __init__(self, master):frame = Frame(master)frame.pack()self.printButton = Button(frame, text="Print Message", command=self.printMessage)self.printButton.pack(side=LEFT)self.quitButton = Button(frame, text="Quit", command=frame.quit)self.quitButton.pack(side=LEFT)def printMessage(self):print "Wow, this actually worked!"root = Tk()
b = BuckysButtons(root)
root.mainloop()010: Python GUI with Tkinter-9-Creating Drop Down Menus1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-9-Creating Drop Down Menus
'''
from Tkinter import *def doNothing():print("ok ok I won't...")root = Tk()menu = Menu(root)
root.config(menu=menu)subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)root.mainloop()011: Python GUI with Tkinter-10-Creating a Toolbar1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-10-Creating a Toolbar
'''
from Tkinter import *def doNothing():print("ok ok I won't...")root = Tk()# ***** Main Menu *****
menu = Menu(root)
root.config(menu=menu)subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)# ***** Toolbar *****
toolbar = Frame(root, bg="blue")insertBtn = Button(toolbar, text="Insert Image", command=doNothing)
insertBtn.pack(side=LEFT, padx=2, pady=2)
printBtn = Button(toolbar, text="Print", command=doNothing)
printBtn.pack(side=LEFT, padx=2, pady=2)toolbar.pack(side=TOP, fill=X)root.mainloop()012: Python GUI with Tkinter-11-Adding the Status Bar1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-11-Adding the Status Bar
'''
from Tkinter import *def doNothing():print("ok ok I won't...")root = Tk()# ***** Main Menu *****
menu = Menu(root)
root.config(menu=menu)subMenu = Menu(menu)
menu.add_cascade(label="File", menu=subMenu)
subMenu.add_command(label="New Project...", command=doNothing)
subMenu.add_command(label="New...", command=doNothing)
subMenu.add_separator()
subMenu.add_command(label="Exit", command=doNothing)editMenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editMenu)
editMenu.add_command(label="Redo", command=doNothing)# ***** Toolbar *****
toolbar = Frame(root, bg="blue")insertBtn = Button(toolbar, text="Insert Image", command=doNothing)
insertBtn.pack(side=LEFT, padx=2, pady=2)
printBtn = Button(toolbar, text="Print", command=doNothing)
printBtn.pack(side=LEFT, padx=2, pady=2)toolbar.pack(side=TOP, fill=X)# ***** Status Bar *****
status = Label(root, text="Preparing to do nothing...", bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, fill=X)root.mainloop()013: Python GUI with Tkinter-12-Messagebox1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-12-Messagebox
How to create a message box with tkinter?
http://stackoverflow.com/questions/1052420/how-to-create-a-message-box-with-tkinter
'''
from Tkinter import *
import tkMessageBoxroot = Tk()
tkMessageBox.showinfo("Window Title", "Monkeys can live up to 300 years.")
msg = tkMessageBoxanswer = msg.askquestion("Question1", "Do you like silly faces?")if answer == 'yes':print(' 8===D~')root.mainloop()014: Python GUI with Tkinter-13-Shapes and Graphics1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-13-Shapes and Graphics
'''
from Tkinter import *root = Tk()canvas = Canvas(root, width=200, height=100)
canvas.pack()blackLine = canvas.create_line(0, 0, 200, 50)
redLine = canvas.create_line(0, 100, 200, 50, fill="red")
greenBox = canvas.create_rectangle(25, 25, 130, 60, fill="green")# canvas.delete(redLine)
canvas.delete(ALL)root.mainloop()015: Python GUI with Tkinter-14-Images and Icons1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding:utf-8 -*-
'''
Python GUI with Tkinter-14-Images and Icons
(1.1)在這個例子中遇到問題"_tkinter.TclError: couldn't recognize data in image file"
(1.2)http://stackoverflow.com/questions/27599311/tkinter-photoimage-doesnt-not-support-png-image
(1.3)http://effbot.org/tkinterbook/photoimage.htm
(1.4)解決辦法參考上面的鏈接. 引入 "from PIL import Image, ImageTk"
---------------
(2)中文目錄要加 u"..."
'''
from Tkinter import *
from PIL import Image, ImageTkroot = Tk()imgList = ["image001.png", "kaola.jpg", u"考拉.jpg"]
print(imgList[-1])
image = Image.open(imgList[-1])
# photo = PhotoImage(file="image001.png")
photo = ImageTk.PhotoImage(image)label = Label(root, image=photo)
label.pack()root.mainloop()
?
posted on 2018-06-06 23:00 秦瑞It行程實(shí)錄 閱讀(...) 評論(...) 編輯 收藏轉(zhuǎn)載于:https://www.cnblogs.com/ruiy/p/9147902.html
總結(jié)
以上是生活随笔為你收集整理的python tkinter chk的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MariaDB 10.3 主主半同步复制
- 下一篇: 【Scala谜题】继承