在界面中很重要的是布局,一开始在查找的的时候网上有很多都是下面这样:
# -*- coding: utf-8 -*-
from Tkinter import *
root = Tk()
# 设置位置窗体长x宽+x坐标+y坐标
root.geometry('80x80+10+10')
# 填充方向
'''
Label(root, text = 'l1', bg = 'red').pack(fill = Y)
Label(root, text = 'l2', bg = 'green').pack(fill = BOTH)
Label(root, text = 'l3', bg = 'blue').pack(fill = X)
# 左右布局
Label(root, text = 'l1', bg = 'red').pack(fill = Y, side = LEFT)
Label(root, text = 'l2', bg = 'green').pack(fill = BOTH, side = RIGHT)
Label(root, text = 'l3', bg = 'blue').pack(fill = X, side = LEFT)
# 绝对布局
l4 = Label(root, text = 'l4')
l4.place(x = 3, y = 3, anchor = NW)
'''
# Grid 网格布局
l1 = Label(root, text = 'l1', bg = 'red')
l2 = Label(root, text = 'l2', bg = 'blue')
l3 = Label(root, text = 'l3', bg = 'green')
l4 = Label(root, text = 'l4', bg = 'yellow')
l5 = Label(root, text = 'l5', bg = 'purple')
l1.grid(row = 0, column = 0)
l2.grid(row = 1, column = 0)
l3.grid(row = 1, column = 1)
l4.grid(row = 2 )
l5.grid(row = 0, column = 3)
root.mainloop()
在写界面的时候,也避免不了使用Entry(输入框)
Eexe = Entry(top, text="选择exe文件", width=40)
Eexe.place(x=135, y=88)
myEn.delete(0, END)
还有就是输入框的插入
myEn.insert(0, text)
获得输入框的内容
myEn.get()
import tkMessageBox
tkMessageBox.showinfo("提示", "提示的内容")
经常使用的还有按钮
Btn1 = Button(top, text="浏览", width=15, command=lambda: myfile(Eexe))
Btn1.place(x=450, y=85)
其中最重要的是command属性,其中
command=lambda: myfile(Eexe)
如果我们的点击事件不需要传入参数,属性可以写成这样
command=myfile
直接等于方法名
在界面中也经常使用文本label
varLabel = StringVar()
# justify = 'left'
labelMessage = Label(top, bg='White', textvariable=varLabel, relief=RAISED, width=45, height=12, wraplength=300,
anchor='n')
labelMessage.place(x=110, y=210)
varLabel.set("想输入的内容")
varLabel.get().encode('utf-8')
使用的python的时候我们经常还要知道当前的操作系统
currentSystem = platform.system()
if currentSystem == 'Windows':