文章目录
- tk1
- tk2
- tk3 添加图片
- tk4 图片作背景
tk1
import tkinter as tk
app=tk.Tk()
app.title("这是标题") # 显示标题
theLabel=tk.Label(app,text='窗口程序!') # 显示文本,图片,图标
theLabel.pack() # 自动调节组件尺寸
app.mainloop()
tk2
import tkinter as tk
class APP:
def __init__(self,master):
frame=tk.Frame(master)
frame.pack(side=tk.LEFT,padx=10,pady=10)
self.hi_there=tk.Button(frame,text='打招呼',fg='white',bg='black',command=self.say_hi)
self.hi_there.pack()
def say_hi(self):
print('大家好!')
root=tk.Tk()
app=APP(root)
root.mainloop()
tk3 添加图片
from tkinter import *
root=Tk()
textLabel = Label(root,text='文字配图片',padx=10)
textLabel.pack(side=LEFT)
photo = PhotoImage(file='photo1.png')
imgLabel = Label(root, image=photo)
imgLabel.pack(side=RIGHT)
mainloop()
tk4 图片作背景
from tkinter import *
root=Tk()
photo = PhotoImage(file='photo1.png')
theLabel = Label(root,text='我是一只大熊猫!\n哈哈哈哈哈!',justify=LEFT,image=photo,compound=CENTER,
font=('微软雅黑',20),
fg='green')
theLabel.pack()
mainloop()