Python Tkinter 快速上手
python 2.7 的範例如下
注意:
注意:
同一個視窗中如果使用grid()就不可使用 pack(),兩者選其一用即可D
import Tkinter as tk
import tkMessageBox
import ttk
class TKDemo():
def __init__(self, master=None):
self.rowCount=0#用這個row count,以免以後修改LAYOUT, 中間要插入新的列,新列之後的row都要改
self.root = master
self.createWidgets()
def createWidgets(self):
label1=tk.Label(self.root, text="First")
label2=tk.Label(self.root, text="Second" )
label1row = self.rowCount
label1.grid(row=label1row, sticky=tk.W)
self.rowCount += 1
label2row = self.rowCount
label2.grid(row=label2row, sticky=tk.W)
self.rowCount += 1
e1 = tk.Entry(self.root)
e2 = tk.Entry(self.root)
photo=tk.PhotoImage(file='ibtn.gif')#png, jpeg, 都不能用。
b=tk.Button(self.root, image=photo, text="ppp", relief=tk.RIDGE)
b.grid(row=label1row, column=2)
#b.config(image=photo)
e1.grid(row=label1row, column=1, sticky="we")#靠東靠西延伸
e2.grid(row=label2row, column=1, columnspan=2, sticky="we")#長度為兩欄,靠東靠西延伸
seprow = self.rowCount
ttk.Separator(self.root,orient=tk.HORIZONTAL).grid(row=seprow, columnspan=3, sticky="we", pady=10)
self.rowCount += 1
var=tk.StringVar()
r1=tk.Radiobutton(self.root,text='Option A',variable=var,value='A')
r2=tk.Radiobutton(self.root,text='Option B',variable=var,value='B')
r3=tk.Radiobutton(self.root,text='Option C',variable=var,value='C')
r1row = self.rowCount
r1.grid(row=r1row, column=1, sticky=tk.W)
self.rowCount += 1
r2row = self.rowCount
r2.grid(row=r2row, column=1, sticky=tk.W)
self.rowCount += 1
r3row = self.rowCount
r3.grid(row=r3row, column=1, sticky=tk.W)
self.rowCount += 1
imageLabel = tk.Label(self.root, image = photo, relief=tk. SUNKEN)
imageLabel.image = photo #沒出現圖要加這行
imageLabel.grid(row=r2row, column=2)
canvas = tk.Canvas(self.root, width =400, height=400, relief=tk.SUNKEN)
xy = 10, 105, 100, 200
canvas.create_arc(xy, start=0, extent=270, fill='gray60')
canvas.create_polygon(0,0,0,400,400,400,400,0, fill='red')
canvas.create_arc(xy, start=270, extent=5, fill='gray70')
canvas.create_arc(xy, start=275, extent=35, fill='gray80')
canvas.create_arc(xy, start=310, extent=49, fill='gray90')
canvas.create_polygon(100,100,285,125,166,177,210,199,205,105, fill='white')
canvas.create_image(145, 280, image=photo, anchor=tk.CENTER)
canvasrow = self.rowCount
canvas.grid(row=canvasrow, column=1)
self.rowCount += 1
scale = tk.Scale(self.root, orient=tk.VERTICAL, length=400, from_=0, to=400,tickinterval=50, relief=tk.SUNKEN)
scale.grid(row=canvasrow, column=2, sticky='NE')
menu = tk.Menu(self.root)
self.root.config(menu=menu)
filemenu = tk.Menu(menu)
editmenu = tk.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
menu.add_cascade(label="Edit", menu=editmenu)
filemenu.add_command(label="New", command=NewFile)
filemenu.add_command(label="Save as...", command=SaveAs)
editmenu.add_command(label="About", command=About)
def mainloop(self):
self.root.mainloop()
def NewFile():
tkMessageBox.showinfo("New", "Creat a new file...")
def SaveAs():
tkMessageBox.showinfo("Save as", "Save as...")
def About():
tkMessageBox.showinfo("About", "This is a tkinter demo.")
if __name__ == '__main__':
root = tk.Tk()
app = TKDemo(master=root)
app.mainloop()
import tkMessageBox
import ttk
class TKDemo():
def __init__(self, master=None):
self.rowCount=0#用這個row count,以免以後修改LAYOUT, 中間要插入新的列,新列之後的row都要改
self.root = master
self.createWidgets()
def createWidgets(self):
label1=tk.Label(self.root, text="First")
label2=tk.Label(self.root, text="Second" )
label1row = self.rowCount
label1.grid(row=label1row, sticky=tk.W)
self.rowCount += 1
label2row = self.rowCount
label2.grid(row=label2row, sticky=tk.W)
self.rowCount += 1
e1 = tk.Entry(self.root)
e2 = tk.Entry(self.root)
photo=tk.PhotoImage(file='ibtn.gif')#png, jpeg, 都不能用。
b=tk.Button(self.root, image=photo, text="ppp", relief=tk.RIDGE)
b.grid(row=label1row, column=2)
#b.config(image=photo)
e1.grid(row=label1row, column=1, sticky="we")#靠東靠西延伸
e2.grid(row=label2row, column=1, columnspan=2, sticky="we")#長度為兩欄,靠東靠西延伸
seprow = self.rowCount
ttk.Separator(self.root,orient=tk.HORIZONTAL).grid(row=seprow, columnspan=3, sticky="we", pady=10)
self.rowCount += 1
var=tk.StringVar()
r1=tk.Radiobutton(self.root,text='Option A',variable=var,value='A')
r2=tk.Radiobutton(self.root,text='Option B',variable=var,value='B')
r3=tk.Radiobutton(self.root,text='Option C',variable=var,value='C')
r1row = self.rowCount
r1.grid(row=r1row, column=1, sticky=tk.W)
self.rowCount += 1
r2row = self.rowCount
r2.grid(row=r2row, column=1, sticky=tk.W)
self.rowCount += 1
r3row = self.rowCount
r3.grid(row=r3row, column=1, sticky=tk.W)
self.rowCount += 1
imageLabel = tk.Label(self.root, image = photo, relief=tk. SUNKEN)
imageLabel.image = photo #沒出現圖要加這行
imageLabel.grid(row=r2row, column=2)
canvas = tk.Canvas(self.root, width =400, height=400, relief=tk.SUNKEN)
xy = 10, 105, 100, 200
canvas.create_arc(xy, start=0, extent=270, fill='gray60')
canvas.create_polygon(0,0,0,400,400,400,400,0, fill='red')
canvas.create_arc(xy, start=270, extent=5, fill='gray70')
canvas.create_arc(xy, start=275, extent=35, fill='gray80')
canvas.create_arc(xy, start=310, extent=49, fill='gray90')
canvas.create_polygon(100,100,285,125,166,177,210,199,205,105, fill='white')
canvas.create_image(145, 280, image=photo, anchor=tk.CENTER)
canvasrow = self.rowCount
canvas.grid(row=canvasrow, column=1)
self.rowCount += 1
scale = tk.Scale(self.root, orient=tk.VERTICAL, length=400, from_=0, to=400,tickinterval=50, relief=tk.SUNKEN)
scale.grid(row=canvasrow, column=2, sticky='NE')
menu = tk.Menu(self.root)
self.root.config(menu=menu)
filemenu = tk.Menu(menu)
editmenu = tk.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
menu.add_cascade(label="Edit", menu=editmenu)
filemenu.add_command(label="New", command=NewFile)
filemenu.add_command(label="Save as...", command=SaveAs)
editmenu.add_command(label="About", command=About)
def mainloop(self):
self.root.mainloop()
def NewFile():
tkMessageBox.showinfo("New", "Creat a new file...")
def SaveAs():
tkMessageBox.showinfo("Save as", "Save as...")
def About():
tkMessageBox.showinfo("About", "This is a tkinter demo.")
if __name__ == '__main__':
root = tk.Tk()
app = TKDemo(master=root)
app.mainloop()
以下是python 中 Tkinter 的所有控件。大概一個2維 GUI 介面能用到的也就下方21種,非常夠用。
- Label
- Button
- Radiobutton
- Checkbutton
- Entry
- Frame
- LabelFrame
- Listbox
- Text
- Message
- PanedWindow
- Scrollbar
- Scale
- Spinbox
- Menu
- OptionMenu
- Menubutton
- Canvas
- Image
- Bitmap
- Toplevel
留言
張貼留言