python自带的tkinter可以自定义,并且可以把事件封装好
#!/usr/bin/python
#-*-coding:utf-8-*-
import tkinter as tk
from tkinter import messagebox
#自定义tkinter类
class my_gui(tk.Frame):
def __init__(self,parent=None):
tk.Frame.__init__(self,parent)
button = tk.Button(self,text='press',command=self.replay)
button.pack()
def replay(self):
messagebox.showinfo(title='hello',message='Button Pressed!')
# def replay():
# messagebox.showinfo(title='hello',message='Button Pressed!')
# window = tk.Tk()
# b1 = tk.Button(window,text='Press',command=replay)
# b1.pack()
# # tk.Label(text='Spam').pack()
# tk.mainloop()
class Customgui(my_gui):
def replay(self):
messagebox.showinfo(title='hahahah', message='From mygui')
if __name__ == '__main__':
mainwindow =tk.Tk()
tk.Label(mainwindow,text=__name__).pack()
popup = tk.Toplevel()
tk.Label(popup,text='Attach').pack(side='left')
Customgui(popup).pack(side='right')
# my_gui(popup).pack(side='right')
# window = my_gui() #生成自定义的Frame,继承tk.Frame
# window.pack()
# window.mainloop()
mainwindow.mainloop()
修改窗口的标题及图标,以及使用lambda来延时button的事件。
#!/usr/bin/python
#-*-coding:utf-8-*-
import tkinter as tk
from tkinter import messagebox
def replay(name):
messagebox.showinfo(title='Replay',message='Hello! ' + '%s'% name)
top = tk.Tk()
#修改标题
top.title('Echo')
#修改左上角图标
top.iconbitmap('../images/my_gui.ico')
tk.Label(top,text='Enter your name:').pack(side='top')
#使用Entry获取用户输入信息
ent = tk.Entry(top)
ent.pack()
#增加lambda来延时对replay函数的调用,使用get方法可以获取输入框的信息(默认是字符串)
btn = tk.Button(top,text='Submit',command=(lambda: replay(ent.get())))
btn.pack(side='left')
top.mainloop()