python模块之tkinter模块简单应用

tkinter模块用来做界面,可以根据我下面的程序做参考。

from tkinter import *  # 导入 Tkinter 库

root = Tk()  # 创建窗口对象的背景色
#######################下面是窗口的一些属性设置############
root.title("hello world")
root.geometry('800x100')
root.resizable(width=True, height=True) #宽不可变, 高可变,默认为True

######################创建一个lable标签的例子#################################
######################用法:Label(根对象, [属性列表])#########################
photo = PhotoImage(file="a.jpg")#file:t图片路径
lable0 = Label(root,
               text = "hell0------------->你好n",
               bg = "red",
               font=("宋体", 20),
               width=1000,
               height=200,
               anchor='center',
               wraplength = '40', #指定text文本中多宽进行换行
               fg = "yellow",#设置字体颜色
               justify = 'center', #设置换行后的对齐方法 left right center
               image=photo #载入图片
               )

lable0.pack(side=TOP) #设置标签的位置  左右上下:LEFT  RTGHT TOP  BOTTOM
###################################################################################


######################Frame:在屏幕上创建一块矩形区域,多作为容器来布局窗体########
for fm in ['red','blue','yellow','green','white','black']:#######################
    framel = Frame(root, bg = fm)#################################################
    #framel.pack(side=BOTTOM, fill='both', ipadx=50, ipady=10, expand=0)##########
    framel.pack(side=BOTTOM, fill='both', ipadx=10, ipady=10)#####################
#################################################################################

####################################Entry(创建单行文本框)########################
var = StringVar()#绑定变量
e = Entry(root, textvariable = var)
var.set("hello")
b = var.get()
b = b + "world!"
var.set(b)
e.pack()
##################################################################################

#################################Text(向该空间内输入文本)#########################
t = Text(root,width=30,height=4)
t.insert(INSERT,'I Love\n')
t.insert(1.0, 'hello\n')
t.insert(INSERT, 'hello000000\n')
t.insert(END, 'nono')
t.pack()
##################################################################################

#################################bottom(创建按钮,用来向Text中输入内容显示)########
def printhello():
    t.insert('1.0', "hello\n")

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

###################################################################################
def print_item(event):
    print
    lb.get(lb.curselection())


var = StringVar()
lb = Listbox(root, listvariable=var)
list_item = [1, 2, 3, 4]  # 控件的内容为1 2 3 4
for item in list_item:
    lb.insert(END, item)
lb.delete(2, 4)  # 此时控件的内容为1 3

#var.set(('a', 'ab', 'c', 'd'))  # 重新设置了,这时控件的内容就编程var的内容了
print(var.get())
lb.bind('<ButtonRelease-1>', print_item)
lb.pack()
####################################################################################


# 创建两个列表
li = ['C', 'python', 'php', 'html', 'SQL', 'java']
movie = ['CSS', 'jQuery', 'Bootstrap']
listb = Listbox(root)  # 创建两个列表组件
listb2 = Listbox(root)
for item in li:  # 第一个小部件插入数据
    listb.insert(0, item)

for item in movie:  # 第二个小部件插入数据a.jpg
    listb2.insert(END, item)

listb.pack()  # 将小部件放置到主窗口中
listb2.pack()
#########################################################################################
#########################载入动态图片####################################################


root.mainloop()  # 进入消息循环

如上代码参考搜索的tkinter模块应用,并进行了代码改良

实现效果如下图:frame暂时还没搞懂。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值