import tkinter
root=tkinter.Tk() # 创建Window窗口对象
root.geometry('300x200+300+300') # 窗口的宽度x高度
root.title('我的第一个GUI程序') # 设置窗口标题
root.config(bg='#fffc11') # 窗口背景颜色
label=tkinter.Label(root,text='hello,python')
label.pack()
button1=tkinter.Button(root,text='BUTTON1')
button1.pack(side=tkinter.LEFT)
button2=tkinter.Button(root,text='BUTTON2')
button2.pack(side=tkinter.RIGHT)
root.mainloop()
代码如下:
from tkinter import *
root =Tk()
root.title('登录')
root.geometry('400x200')
root.config(bg='#ffcc00')
Label(root,text='用户名',width=6).place(x=1,y=1)
Entry(root,width=20).place(x=45,y=1)
Label(root,text='密码',width=6).place(x=1,y=20)
Entry(root,width=20,show='*').place(x=45,y=20)
Button(root,text='登录',width=8).place(x=40,y=40)
Button(root,text='取消',width=8).place(x=110,y=40)
root.mainloop()
# grid布局.py
from tkinter import *
root = Tk()
root.title('计算器示例')
root.geometry('300x150+280+280')
root.config(bg='#cc66ff')
L1 = Button(root,text='1',width=5,bg='yellow')
L2 = Button(root,text='2',width=5,bg='blue')
L3 = Button(root,text='3',width=5,bg='red')
L4 = Button(root,text='4',width=5,bg='green')
L5 = Button(root,text='5',width=5,bg='orange')
L6 = Button(root,text='6',width=5,bg='cyan')
L7 = Button(root,text='7',width=5,bg='purple')
L8 = Button(root,text='8',width=5,bg='pink')
L9 = Button(root,text='9',width=5,bg='red')
L0 = Button(root,text='0',width=5,bg='yellow')
Lp = Button(root,text='.',bg='pink')
L1.grid(row=0,column=0)
L2.grid(row=0,column=1)
L3.grid(row=0,column=2)
L4.grid(row=1,column=0)
L5.grid(row=1,column=1)
L6.grid(row=1,column=2)
L7.grid(row=2,column=0)
L8.grid(row=2,column=1)
L9.grid(row=2,column=2)
L0.grid(row=3,column=0,columnspan=2,sticky=E+W)
Lp.grid(row=3,column=2,sticky=E+W)
root.mainloop()