您的位置:首页 > 编程语言 > Python开发

Python -- Gui编程 -- Tkinter的使用 -- 基本控件

2014-05-04 16:36 781 查看
1.按钮

tkBtton.py

import tkinter

root = tkinter.Tk()
btn1 = tkinter.Button(root, anchor=tkinter.E,\
text='Button1', width=40, height=5)
btn1.pack()
btn2 = tkinter.Button(root, \
text='Button2', bg='blue')
btn2.pack()
btn3 = tkinter.Button(root, \
text='Button3', width=14, height=1)
btn3.pack()
btn4 = tkinter.Button(root, \
text='Button4', width=60, height=5, state=tkinter.DISABLED)
btn4.pack()
root.mainloop()




2.标签

tkLabel.py

import tkinter

root = tkinter.Tk()

lbl1 = tkinter.Label(root, anchor=tkinter.E,
bg='blue', fg='red', text='Python', width=30, height=5)
lbl1.pack()

lbl2 = tkinter.Label(root, text='Python GUI\nTkinter',
justify=tkinter.LEFT, width=30, height=5)
lbl2.pack()

lbl3 = tkinter.Label(root, text='Python GUI\nTkinter',
justify=tkinter.RIGHT, width=30, height=5)
lbl3.pack()

lbl4 = tkinter.Label(root, text='Python GUI\nTkinter',
justify=tkinter.CENTER, width=30, height=5)
lbl4.pack()

root.mainloop()




3.单选框、复选框

tkCheck.py

import tkinter

root = tkinter.Tk()

r = tkinter.StringVar()
r.set('1')
radio = tkinter.Radiobutton(root, variable=r, value='1', text='Radio1')
radio.pack()

radio = tkinter.Radiobutton(root, variable=r, value='2', text='Radio2')
radio.pack()

radio = tkinter.Radiobutton(root, variable=r, value='3', text='Radio3')
radio.pack()

radio = tkinter.Radiobutton(root, variable=r, value='4', text='Radio4')
radio.pack()

c = tkinter.IntVar()
c.set(1)
check = tkinter.Checkbutton(root, text='CheckButton', variable=c, onvalue=1, offvalue=2)
check.pack()

root.mainloop()
print(r.get())
print(c.get())




4.单选框、复选框的平坦样式

tkRCButton.py

import tkinter

root = tkinter.Tk()

r = tkinter.StringVar()
r.set('1')
radio = tkinter.Radiobutton(root, variable=r, value='1', text='Radio1', indicatoron=0)
radio.pack()

radio = tkinter.Radiobutton(root, variable=r, value='2', text='Radio2', indicatoron=0)
radio.pack()

radio = tkinter.Radiobutton(root, variable=r, value='3', text='Radio3', indicatoron=0)
radio.pack()

radio = tkinter.Radiobutton(root, variable=r, value='4', text='Radio4', indicatoron=0)
radio.pack()

c = tkinter.IntVar()
c.set(1)
check = tkinter.Checkbutton(root, text='CheckButton', variable=c, onvalue=1, offvalue=2, indicatoron=0)
check.pack()

root.mainloop()
print(r.get())
print(c.get())




5.文本框

tkEntry.py

import tkinter

root = tkinter.Tk()

entry1 = tkinter.Entry(root, show='*' )
entry1.pack()

entry2 = tkinter.Entry(root, show='#', width='50')
entry2.pack()

entry3 = tkinter.Entry(root, bg='red', fg='blue')
entry3.pack()

entry4 = tkinter.Entry(root, selectbackground='red',\
selectforeground='gray')
entry4.pack()

entry5 = tkinter.Entry(root, state=tkinter.DISABLED)
entry5.pack()

edit1 = tkinter.Text(root, selectbackground='red',
selectforeground='gray')
edit1.pack()

root.mainloop()


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: