暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

tkinter基础控件编程

老张的技术博客 2017-12-16
290

需注意:P3之前为Tkinter,P3之后为tkinter

基本程序架构


from tkinter import * //导入tkinter库

root = Tk() //创建主窗口

root.mainloop() //消息循环,用作程序结束

设置窗口标题


from tkinter import *

root = Tk()

root.title("hello world") //设置标题

root.geometry('200x100') //设置窗口大小

root.resizable(width=False, height=True) //宽可变,高不可变

root.mainloop()

  • title: 设置窗口标题

  • geometry: 设置窗口大小

  • resizable():设置窗口是否可以变化长 宽

label控件


from tkinter import *

root = Tk()

root.title("hello world")

root.geometry('200x100')

root.resizable(width=False, height=True)

l = Label(root, text="show", bg="green", font=("Arial", 12),width=5, height=2)//其中的root表示label属于哪个窗口

l.pack(side=LEFT) //控件靠左排列

root.mainloop()

frame控件


from tkinter import *

root = Tk()

root.title("hello world")

root.geometry('200x100')

Label(root, text="校训", font=("Arial", 15)).pack(side=TOP)//属于根窗口

frm = Frame(root)

frm_L = Frame(frm)//定义左frame,括号内表示此空间在哪个窗口

Label(frm_L, text="厚德", font=("Arial", 15)).pack(side=TOP)

Label(frm_L, text="博学", font=("Arial", 15)).pack(side=TOP)

frm_L.pack(side=LEFT)//左frame打包

frm_R = Frame(frm)//定义右frame

Label(frm_R, text="敬业", font=("Arial", 15)).pack(side=TOP)

Label(frm_R, text="乐群", font=("Arial", 15)).pack(side=TOP)

frm_R.pack(side=RIGHT)//右frame打包

frm.pack()//整体窗口打包

root.mainloop()

Entry控件


from tkinter import *

root = Tk()

root.title("hello world")

root.geometry()//为设置参数则为默认大小

var = StringVar()

e = Entry(root, textvariable = var)

var.set("hello")

e.pack()

root.mainloop()

  •   创建:lb =Entry(根对象, [属性列表])

  •   绑定变量 var=StringVar()    lb=Entry(根对象, textvariable = var)

  •   获取文本框中的值   var.get()

  •   设置文本框中的值   var.set(item1)

Text控件


from tkinter import *

root = Tk()

root.title("hello world")

root.geometry('300x200')

t = Text(root)

t.insert(1.0, 'hello\n')

t.insert(END, 'hello000000\n')

t.insert(END, 'nono')

t.pack()

root.mainloop()


说明

  向该空间内输入文本

用法

  t = Text(根对象)

  插入:t.insert(mark, 内容)

  删除:t.delete(mark1, mark2)

  其中,mark可以是行号,或者特殊标识,例如

  • INSERT:光标的插入点CURRENT:鼠标的当前位置所对应的字符位置

  • END:这个Textbuffer的最后一个字符

  • SEL_FIRST:选中文本域的第一个字符,如果没有选中区域则会引发异常

  • SEL_LAST:选中文本域的最后一个字符,如果没有选中区域则会引发 异常

button控件


from tkinter import *

root = Tk()

root.title("hello world")

root.geometry()

def printhello():

   t.insert('1.0', "hello\n")

t = Text()

t.pack()

Button(root, text = "press", command = printhello).pack()

root.mainloop()

Button(根对象, [属性列表])

ListBox控件


from tkinter import *

root = Tk()

root.title("hello world")

root.geometry()

def print_item(event):

   print(lb.get(lb.curselection())) //获得所选中的选项

var = StringVar()

lb = Listbox(root, listvariable = var) //创建listbox对象,属于root窗口,传值为var(字符串)

list_item = [1, 2, 3, 4]

for item in list_item:

   lb.insert(END, item) //此时控件内容为1,2,3,4

lb.delete(2, 4) //删除2,4

var.set(('a', 'ab', 'c', 'd')) //设置列表的所有值

print(var.get()) //获取列表的所有值

lb.bind('<ButtonRelease-1>', print_item) //绑定对象

lb.pack() //打包控件

root.mainloop() //消息循环

用法

  •   创建:lb = ListBox(根对象, [属性列表])

  •   绑定变量 var=StringVar()    lb=ListBox(根对象, listvariable = var)

  •   得到列表中的所有值   var.get()

  •   设置列表中的所有值   var.set((item1, item2, .....))

  •   添加:lb.insert(item)

  •   删除:lb.delete(item,...)

  •   绑定事件 lb.bind('<ButtonRelease-1>', 函数)

  •   获得所选中的选项 lbl.get(lb.curselection())

属性

  selectmode可以为BROWSE MULTIPL SINGLE(单选或多选)

Scrollbar控件


from tkinter import *

root = Tk()

root.title("hello world")

root.geometry()

def print_item(event):

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

var = StringVar()

lb = Listbox(root, height = 5, selectmode = BROWSE, listvariable = var)

lb.bind('<ButtonRelease-1>', print_item)

list_item = [1,2,3,4,5,6,7,8,9,0]

for item in list_item:

   lb.insert(END, item)

scr1 = Scrollbar(root) //创建scrollbar控件

scr1.pack(side=RIGHT, fill=Y) //scrollbar控件居右、全屏设置

lb.configure(yscrollcommand = scr1.set) //scrollbar匹配listbox

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

scr1['command'] = lb.yview //当鼠标离开scrollbar时就调用listbox的窗口

root.mainloop()

yscrollcommand

指定一个用来与垂直滚动框进行信息交流的命令前缀,当控件窗口视图改变(或者别的任何滚动条显示的改变,如控件的总尺寸改变等等),控件将通过把滚动命令和两个数连接起来产生一个命令.

command

指定一个与控件关联的命令.该命令通常在鼠标离开控件之时被调用,


文章转载自老张的技术博客,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论