Python GUI中tkinter控件怎么用

发布时间:2021-05-20 10:36:01 作者:小新
来源:亿速云 阅读:178

这篇文章主要介绍了Python GUI中tkinter控件怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

具体如下:

相关内容:

首发时间:2018-03-04 16:39

Python的GUI各有各特点。

由于只是轻微涉及GUI开发,所以就以轻量级的tkinter来学习。

tkinter的使用:

from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('500x500')
root.mainloop()

Button所有的可设置参数

activebackground, activeforeground, anchor,
background, bitmap, borderwidth, cursor,
disabledforeground, font, foreground
highlightbackground, highlightcolor,
highlightthickness, image, justify,
padx, pady, relief, repeatdelay,
repeatinterval, takefocus, text,
textvariable, underline, wraplength

WIDGET-SPECIFIC OPTIONS【特有选项】: command, compound, default, height, overrelief, state, width

from tkinter import *

def hello():
 print("hello")
root=Tk()

# RELIEF=["flat", "raised", "sunken", "solid", "ridge", "groove"]

btn1=Button(root,text='click me')
btn1.config(bg='green',fg='white',cursor='hand1',height=10,width=10,command=hello,relief='sunken')
btn1.config(anchor=LEFT)
btn1.pack()

# for col,i in enumerate(RELIEF):
#  btn=Button(root,text=i,relief=i,anchor=S)
#  btn.grid(row=0,column=col)

root.mainloop()
STANDARD OPTIONS【label的标准可选参数】

 activebackground, activeforeground, anchor,
 background, bitmap, borderwidth, cursor,
 disabledforeground, font, foreground,
 highlightbackground, highlightcolor,
 highlightthickness, image, justify,
 padx, pady, relief, takefocus, text,
 textvariable, underline, wraplength
WIDGET-SPECIFIC OPTIONS【特有选项】:

 height, state, width
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('500x500')

label=Label(text='用户名:',bg='green')
label.grid()
root.mainloop()

Python GUI中tkinter控件怎么用

STANDARD OPTIONS【标准可用参数】

 activebackground, activeforeground, anchor,
 background, bitmap, borderwidth, cursor,
 disabledforeground, font, foreground,
 highlightbackground, highlightcolor,
 highlightthickness, image, justify,
 padx, pady, relief, takefocus, text,
 textvariable, underline, wraplength
#这是一段没有显示Frame 代码
from tkinter import *

root=Tk()
root.geometry('500x500')
frame=Frame(root,height = 200,width = 400,bg = 'black')

Label(frame,text='mylabel').pack()

frame.pack()

root.mainloop()
#下面是探究出缩水原因的代码
from tkinter import *

root=Tk()
root.geometry('500x500')
frame=Frame(root,height = 400,width = 400,bg = 'green')


button1=Button(frame,text='hello')
button1.pack(side=LEFT,padx=5,pady=5)#增加了边距之后,发现出了frame的背景颜色
button2=Button(frame,text='hello')
button2.pack(side=LEFT)
frame.pack(side=TOP)


root.mainloop()

Python GUI中tkinter控件怎么用

#下面的是使用.pack_propagate(0)解决了问题的代码
from tkinter import *

root=Tk()
root.geometry('500x500')
frame=Frame(root,height = 400,width = 400,bg = 'green')

# Label(frame,text='mylabel',padx=5,pady=5).pack(side=LEFT)
button1=Button(frame,text='hello')
button1.pack(side=LEFT,padx=5,pady=5)
button2=Button(frame,text='hello')
button2.pack(side=LEFT)
frame.pack_propagate(0)
frame.pack(side=TOP)

# frame.pack(side=TOP,fill=X)

root.mainloop()
Valid resource names:
background, bd, bg, borderwidth, class,
colormap, container, cursor, height, highlightbackground,
highlightcolor, highlightthickness, menu, relief, screen, takefocus,
use, visual, width
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('500x500')

t1=Toplevel(root)
t1.title("Top窗口")
t1.geometry("100x100")
label=Label(t1,text="用户名:")
label.pack()
root.mainloop()
常见可用参数:
activebackground, activeborderwidth,
activeforeground, background, bd, bg, borderwidth, cursor,
disabledforeground, fg, font, foreground, postcommand, relief,
selectcolor, takefocus, tearoff, tearoffcommand, title, type
from tkinter import *

root=Tk()

menuBar=Menu(root,tearoff=0)
root.config(menu=menuBar)
filemenu=Menu(menuBar,fg='green')#文件菜单下的字体是绿色的
filemenu.add_command(label='新建',accelerator = 'Ctrl+N')
filemenu.add_command(label='打开',accelerator = 'Ctrl+O')
filemenu.add_command(label='保存',accelerator = 'Ctrl+S')
filemenu.add_command(label='另存为',accelerator ='Ctrl+Shift+S')
menuBar.add_cascade(label='文件',menu=filemenu)

#这里测试root.config(menu=menuBar)的作用
# def show_menuBar():
#  root.config(menu=menuBar)
# button=Button(text='show_menu',command=show_menuBar)
# button.pack()


root.mainloop()
from tkinter import *
root=Tk()
menubtn=Menubutton(root,text='单击出现下拉菜单',relief='raise')#建立一个菜单按钮
menubtn.pack()
#添加菜单
filemenu=Menu(menubtn)
filemenu.add_command(label='新建')

menubtn.config(menu=filemenu)#设置菜单按钮允许显示菜单,这里不是root了
root.mainloop()
可用参数: background, bd, bg, borderwidth, closeenough,
confine, cursor, height, highlightbackground, highlightcolor,
highlightthickness, insertbackground, insertborderwidth,
insertofftime, insertontime, insertwidth, offset, relief,
scrollregion, selectbackground, selectborderwidth, selectforeground,
state, takefocus, width, xscrollcommand, xscrollincrement,
yscrollcommand, yscrollincrement
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('500x500')
mycanvas=Canvas(root,width=200,height=200,bg='green')
mycanvas.pack()
#画一个矩形
mycanvas.create_rectangle(10,10,110,110,outline = 'red',width = 5)

root.mainloop()
Valid resource names: background, bd, bg, borderwidth, cursor,
exportselection, fg, font, foreground, highlightbackground,
highlightcolor, highlightthickness, insertbackground,
insertborderwidth, insertofftime, insertontime, insertwidth,
invalidcommand, invcmd, justify, relief, selectbackground,
selectborderwidth, selectforeground, show, state, takefocus,
textvariable, validate, validatecommand, vcmd, width,
xscrollcommand.
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('300x300+20+10')
entry=Entry(root)
entry.pack()

root.mainloop()
STANDARD OPTIONS

 background, borderwidth, cursor,
 exportselection, font, foreground,
 highlightbackground, highlightcolor,
 highlightthickness, insertbackground,
 insertborderwidth, insertofftime,
 insertontime, insertwidth, padx, pady,
 relief, selectbackground,
 selectborderwidth, selectforeground,
 setgrid, takefocus,
 xscrollcommand, yscrollcommand,

WIDGET-SPECIFIC OPTIONS

 autoseparators, height, maxundo,
 spacing1, spacing2, spacing3,
 state, tabs, undo, width, wrap,
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('300x300+250+55')
button=Button(text='submit')
button.pack()
t1=Text(root,height=100,width=100,cursor='cross')
t1.pack()

root.mainloop()
Valid resource names: background, bd, bg, borderwidth, cursor,
exportselection, fg, font, foreground, height, highlightbackground,
highlightcolor, highlightthickness, relief, selectbackground,
selectborderwidth, selectforeground, selectmode, setgrid, takefocus,
width, xscrollcommand, yscrollcommand, listvariable
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('300x300+20+10')
# listbox=Listbox(root)
listbox=Listbox(root,selectmode=EXTENDED)
listbox.insert(0,"孙悟空")
listbox.insert(1,"唐僧")
listbox.insert(2,"葫芦娃")
listbox.pack()
def func1():
 print(listbox.get(0,END))#以元组形式返回所有listbox的元素
def func2():
 print(listbox.select_includes(1))#当对应索引被选中时返回True
def func3():
 print(listbox.curselection())#以元组形式返回被选中的元素

btn1=Button(text="获取所有元素",command=func1)
btn1.pack()
btn2=Button(text="判断1是否选中",command=func2)
btn2.pack()
btn3=Button(text="获取选中的索引",command=func3)
btn3.pack()


root.mainloop()
可用参数:activebackground, activeforeground, anchor,
background, bd, bg, bitmap, borderwidth, command, cursor,
disabledforeground, fg, font, foreground, height,
highlightbackground, highlightcolor, highlightthickness, image,
indicatoron, justify, offvalue, onvalue, padx, pady, relief,
selectcolor, selectimage, state, takefocus, text, textvariable,
underline, variable, width, wraplength
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('200x200')
def submit():
 print('男:',v1.get(),'女:',v2.get(),'另外:',v3.get())#选择则值为1,不选中为0
 # pass


v1 = IntVar()   #用tkinter变量来表示按钮是否选中
v2 = IntVar()
v3 = IntVar()
# 使用 Checkbutton时,必须创建一个 Tkinter 变量用于存放按钮的状态:
cbtn=Checkbutton(root,text='男',variable=v1,command=submit)
cbtn2=Checkbutton(root,text='女',variable=v2,command=submit)
#v3是为了测试variable相同时,点一个,所有的v3都被选中
cbtn3=Checkbutton(root,text='不明',variable=v3,command=submit)
cbtn4=Checkbutton(root,text='保密',variable=v3,command=submit)

button=Button(text='submit',command=submit)
button.pack()
cbtn.pack()
cbtn2.pack()
cbtn3.pack()
cbtn4.pack()
root.mainloop()
Valid resource names: activebackground, activeforeground, anchor,
background, bd, bg, bitmap, borderwidth, command, cursor,
disabledforeground, fg, font, foreground, height,
highlightbackground, highlightcolor, highlightthickness, image,
indicatoron, justify, padx, pady, relief, selectcolor, selectimage,
state, takefocus, text, textvariable, underline, value, variable,
width, wraplength
from tkinter import *

root=Tk()
v=StringVar()
l=['man','woman','unknow']
def ptr():
 print(v.get())
for i in l:
 rbtn=Radiobutton(root,text=i,variable=v,value=i,command=ptr)
 rbtn.pack()

root.mainloop()
Valid resource names:
activebackground, background, bigincrement, bd,
bg, borderwidth, command, cursor, digits, fg, font, foreground, from,
highlightbackground, highlightcolor, highlightthickness, label,
length, orient, relief, repeatdelay, repeatinterval, resolution,
showvalue, sliderlength, sliderrelief, state, takefocus,
tickinterval, to, troughcolor, variable, width
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('300x300+20+10')
scale=Scale(root,from_=0, to=100)#默认是竖的
scale2=Scale(root,from_=0, to=100,orient=HORIZONTAL)#横的
scale.pack()
scale2.pack()

root.mainloop()
Valid resource names:
activebackground, activerelief,
background, bd, bg, borderwidth, command, cursor,
elementborderwidth, highlightbackground,
highlightcolor, highlightthickness, jump, orient,
relief, repeatdelay, repeatinterval, takefocus,
troughcolor, width.
from tkinter import *

root=Tk()
root.title('我的窗口')
root.geometry('300x300+250+55')
button=Button(text='submit')
button.pack()
t1=Text(root,height=100,width=100,cursor='cross')

slb=Scrollbar(root)
slb.pack(side=RIGHT,fill=Y)#设置滚动条的显示形式
t1.config(yscrollcommand=slb.set)#设置允许滚动条
#由于没有绑定事件,所以直接拖拽滚动条无效

t1.pack()
root.mainloop()

python的数据类型有哪些?

python的数据类型:1. 数字类型,包括int(整型)、long(长整型)和float(浮点型)。2.字符串,分别是str类型和unicode类型。3.布尔型,Python布尔类型也是用于逻辑运算,有两个值:True(真)和False(假)。4.列表,列表是Python中使用最频繁的数据类型,集合中可以放任何数据类型。5. 元组,元组用”()”标识,内部元素用逗号隔开。6. 字典,字典是一种键值对的集合。7. 集合,集合是一个无序的、不重复的数据组合。

感谢你能够认真阅读完这篇文章,希望小编分享的“Python GUI中tkinter控件怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. python导入tkinter的方法
  2. Python中Tkinter Entry和Text怎么用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:怎么解决执行Python程序时模块报错问题

下一篇:Python生成器常见问题有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》