Abstract: OOP designs and quit buttons
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 2 12:35:28 2015
@author: yuan
"""
import sys
from Tkinter import *
from tkMessageBox import *
class MyWindow:
def __init__(self, title='tkinter'):
self.win=Tk()
self.win.title(title)
self.title=title
#add a label
def win_label(self, txt):
widget=Label(self.win, text=txt)
widget.config(bg='green', fg='black')
widget.config(font=('times', 20, 'normal') )
widget.config(height=5, width=25)
widget.pack(expand=YES, fill=BOTH)
return self.win
#add a button
def config_button(self, widget, txt):
widget.config(text=txt)
widget.config(cursor='gumby')
widget.config(bd=5, relief=RAISED)
widget.config(bg='cyan', fg='black')
widget.config(font=('helvetica', 20, 'bold') )
widget.pack(padx=10, pady=10)
widget.pack(side=LEFT, expand=YES)
return widget
def exec_button(self, label, func):
widget=Button(self.win, command=func)
widget=self.config_button(widget, label)
return self.win
def callback(self):
if askyesno('verify', 'do your really want to quit?'):
showwarning('Yes', "Quit not yet implemented")
else:
showinfo('No', "Quit has been cancelled")
def quit(self):
ans=askokcancel('verify exit', 'really quit?')
if ans:
Frame.quit(self)
###main program
#the simplest label window
ww=MyWindow(title='Tkinter')
#the window will be present on the left top corner of the screen
#win=ww.win_v1('Hello GUI world')
#some decorations of labels and titles
win=ww.win_label('Hello world!')
win=ww.win_label('Good luck!')
#add two buttons
win=ww.exec_button('warning', ww.callback )
win=ww.exec_button('quit', sys.exit )
win=ww.exec_button('close', win.destroy)
win.mainloop()
No comments:
Post a Comment