一个GUI的模板
from tkinter import messagebox
from tkinter import *
class Appliccation(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.pack()
self.createWidget()
def createWidget(self):
"""创建组件"""
def login(self):
messagebox.showinfo("登录")
if __name__ == '__main__':
root=Tk()
root.geometry("400x100+200+300")#设置宽高
root.title("模板")
app = Appliccation(master=root)
root.mainloop()
多个组件尝试
from tkinter import *
from tkinter import messagebox
class Appliccation(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.createWidget()
def createWidget(self):
"""创建组件"""
self.labe01=Label(self, text="Label01", width=10, height=2,
bg="black", fg="white")#分别表示文本,宽度,高度,背景色,文本色
self.labe01.pack()
self.labe02 = Label(self, text="Label02", width=10, height=2,
bg="blue", fg="white",font=("黑体,30"))
self.labe02.pack()
# 显示图像
global photo #声明成全局变量,不会销毁,局部的话执行完就销毁了
photo=PhotoImage(file="gaitubao_180822150040_gif.gif")#photo指向一个图片对象
#图片对象只能是gif格式的图片
self.labe03=Label(self,image=photo)#通过image将图片赋到labe03上面
self.labe03.pack()#水平的显示
# 显示一行字
self.labe04=Label(self,text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",borderwidth=5,relief="solid",justify="right")
self.labe04.pack()
#button
self.button1=Button(self)#定义button
self.button1["text"]="hello"#button里面的文字
self.button1.pack()
self.button1["command"]=self.songhua()#绑定事件
def songhua(self):#写一下事件
messagebox.showinfo("HELLO","hello world")
if __name__ == '__main__':
root = Tk()
root.geometry("1000x600+400+200")#设置宽度高度
root.title("经典模板")标题
app = Appliccation(master=root)
root.mainloop()
极简登录页面
from tkinter import messagebox
from tkinter import *
class Appliccation(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.pack()
self.createWidget()
def createWidget(self):
"""创建组件"""
self.label01=Label(self, text="用户名")
self.label01.pack()
#绑定到指定的组件
v1 = StringVar#输入的东西设置为字符串类型
# v1.set('admin')
self.entry01=Entry(self, textvariable=v1)#输入框
self.entry01.pack()#显示
# print(v1.get())
#密码
self.label02 = Label(self, text="密码")
self.label02.pack()
# 绑定到指定的组件
v2 = StringVar
# v1.set('admin')
self.entry02 = Entry(self, textvariable=v2,show="*")#通过show显示的密码都是*号
self.entry02.pack()
#登录
self.btn01=Button(self,text="登录",command=self.login)
self.btn01.pack()
def login(self):
yonghuming=self.entry01.get()#获得输入的用户名
mima=self.entry02.get()#获得输入的密码
print("用户名:"+yonghuming)
print("密码:"+mima)
messagebox.showinfo("登录","登录成功")
if __name__ == '__main__':
root=Tk()
root.geometry("250x120+200+300")
root.title("登录页面")#程序框的标题
app = Appliccation(master=root)
root.mainloop()