Python教程 - Tkinter 登陆界面示例
完整代码:
import tkinter
import tkinter.messagebox
import random
def btn_click():
if e1.get()=='xiaoming' and e2.get()=='123456':
tkinter.messagebox.showinfo('tips', 'Correct')
else:
tkinter.messagebox.showerror('tips', 'Error')
main = tkinter.Tk()
main.title('Hello tkinter')
main.geometry('300x200')
l1 = tkinter.Label(main, text='User name:')
l1.config(height=2)
l1.config(width=20)
l1.pack()
e1 = tkinter.Entry(main)
e1.pack()
l2 = tkinter.Label(main, text='Password:')
l2.config(height=2)
l2.config(width=20)
l2.pack()
e2 = tkinter.Entry(main)
e2.pack()
b = tkinter.Button(main,text='OK')
b.config(bg='green')
b.config(command=btn_click)
b.pack()
main.mainloop()
使用Grid布局优化:
完整代码:
import tkinter
import tkinter.messagebox
import random
def btn_click():
if e1.get()=='xiaoming' and e2.get()=='123456':
tkinter.messagebox.showinfo('tips', 'Correct')
else:
tkinter.messagebox.showerror('tips', 'Error')
main = tkinter.Tk()
main.title('Hello tkinter')
main.geometry('300x200')
l1 = tkinter.Label(main, text='User name:')
l1.config(height=2)
l1.config(width=20)
l1.grid(row=0, column=0)
e1 = tkinter.Entry(main)
e1.grid(row=0, column=1)
l2 = tkinter.Label(main, text='Password:')
l2.config(height=2)
l2.config(width=20)
l2.grid(row=1, column=0)
e2 = tkinter.Entry(main)
e2.grid(row=1, column=1)
b = tkinter.Button(main,text='OK')
b.config(bg='green')
b.config(command=btn_click)
b.grid(row=2, column=1)
main.mainloop()