## An Introduction to Tkinter## Copyright (c) 1997 by Fredrik Lundh## This copyright applies to Dialog, askinteger, askfloat and asktring## fredrik@pythonware.com# http://www.pythonware.com#"""This modules handles dialog boxes.It contains the following public symbols:SimpleDialog -- A simple but flexible modal dialog boxDialog -- a base class for dialogsaskinteger -- get an integer from the useraskfloat -- get a float from the useraskstring -- get a string from the user"""from tkinter import *from tkinter import messageboximport tkinter # used at _QueryDialog for tkinter._default_rootclass SimpleDialog:def __init__(self, master,text='', buttons=[], default=None, cancel=None,title=None, class_=None):if class_:self.root = Toplevel(master, class_=class_)else:self.root = Toplevel(master)if title:self.root.title(title)self.root.iconname(title)self.message = Message(self.root, text=text, aspect=400)self.message.pack(expand=1, fill=BOTH)self.frame = Frame(self.root)self.frame.pack()self.num = defaultself.cancel = cancelself.default = defaultself.root.bind('<Return>', self.return_event)for num in range(len(buttons)):s = buttons[num]b = Button(self.frame, text=s,command=(lambda self=self, num=num: self.done(num)))if num == default:b.config(relief=RIDGE, borderwidth=8)b.pack(side=LEFT, fill=BOTH, expand=1)self.root.protocol('WM_DELETE_WINDOW', self.wm_delete_window)self._set_transient(master)def _set_transient(self, master, relx=0.5, rely=0.3):widget = self.rootwidget.withdraw() # Remain invisible while we figure out the geometrywidget.transient(master)widget.update_idletasks() # Actualize geometry informationif master.winfo_ismapped():m_width = master.winfo_width()m_height = master.winfo_height()m_x = master.winfo_rootx()m_y = master.winfo_rooty()else:m_width = master.winfo_screenwidth()m_height = master.winfo_screenheight()m_x = m_y = 0w_width = widget.winfo_reqwidth()w_height = widget.winfo_reqheight()x = m_x + (m_width - w_width) * relxy = m_y + (m_height - w_height) * relyif x+w_width > master.winfo_screenwidth():x = master.winfo_screenwidth() - w_widthelif x < 0:x = 0if y+w_height > master.winfo_screenheight():y = master.winfo_screenheight() - w_heightelif y < 0:y = 0widget.geometry("+%d+%d" % (x, y))widget.deiconify() # Become visible at the desired locationdef go(self):self.root.wait_visibility()self.root.grab_set()self.root.mainloop()self.root.destroy()return self.numdef return_event(self, event):if self.default is None:self.root.bell()else:self.done(self.default)def wm_delete_window(self):if self.cancel is None:self.root.bell()else:self.done(self.cancel)def done(self, num):self.num = numself.root.quit()class Dialog(Toplevel):'''Class to open dialogs.This class is intended as a base class for custom dialogs'''def __init__(self, parent, title = None):'''Initialize a dialog.Arguments:parent -- a parent window (the application window)title -- the dialog title'''Toplevel.__init__(self, parent)self.withdraw() # remain invisible for now# If the master is not viewable, don't# make the child transient, or else it# would be opened withdrawnif parent.winfo_viewable():self.transient(parent)if title:self.title(title)self.parent = parentself.result = Nonebody = Frame(self)self.initial_focus = self.body(body)body.pack(padx=5, pady=5)self.buttonbox()if not self.initial_focus:self.initial_focus = selfself.protocol("WM_DELETE_WINDOW", self.cancel)if self.parent is not None:self.geometry("+%d+%d" % (parent.winfo_rootx()+50,parent.winfo_rooty()+50))self.deiconify() # become visible nowself.initial_focus.focus_set()# wait for window to appear on screen before calling grab_setself.wait_visibility()self.grab_set()self.wait_window(self)def destroy(self):'''Destroy the window'''self.initial_focus = NoneToplevel.destroy(self)## construction hooksdef body(self, master):'''create dialog body.return widget that should have initial focus.This method should be overridden, and is calledby the __init__ method.'''passdef buttonbox(self):'''add standard button box.override if you do not want the standard buttons'''box = Frame(self)w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)w.pack(side=LEFT, padx=5, pady=5)w = Button(box, text="Cancel", width=10, command=self.cancel)w.pack(side=LEFT, padx=5, pady=5)self.bind("<Return>", self.ok)self.bind("<Escape>", self.cancel)box.pack()## standard button semanticsdef ok(self, event=None):if not self.validate():self.initial_focus.focus_set() # put focus backreturnself.withdraw()self.update_idletasks()try:self.apply()finally:self.cancel()def cancel(self, event=None):# put focus back to the parent windowif self.parent is not None:self.parent.focus_set()self.destroy()## command hooksdef validate(self):'''validate the dataThis method is called automatically to validate the data before thedialog is destroyed. By default, it always validates OK.'''return 1 # overridedef apply(self):'''process the dataThis method is called automatically to process the data, *after*the dialog is destroyed. By default, it does nothing.'''pass # override# --------------------------------------------------------------------# convenience dialoguesclass _QueryDialog(Dialog):def __init__(self, title, prompt,initialvalue=None,minvalue = None, maxvalue = None,parent = None):if not parent:parent = tkinter._default_rootself.prompt = promptself.minvalue = minvalueself.maxvalue = maxvalueself.initialvalue = initialvalueDialog.__init__(self, parent, title)def destroy(self):self.entry = NoneDialog.destroy(self)def body(self, master):w = Label(master, text=self.prompt, justify=LEFT)w.grid(row=0, padx=5, sticky=W)self.entry = Entry(master, name="entry")self.entry.grid(row=1, padx=5, sticky=W+E)if self.initialvalue is not None:self.entry.insert(0, self.initialvalue)self.entry.select_range(0, END)return self.entrydef validate(self):try:result = self.getresult()except ValueError:messagebox.showwarning("Illegal value",self.errormessage + "\nPlease try again",parent = self)return 0if self.minvalue is not None and result < self.minvalue:messagebox.showwarning("Too small","The allowed minimum value is %s. ""Please try again." % self.minvalue,parent = self)return 0if self.maxvalue is not None and result > self.maxvalue:messagebox.showwarning("Too large","The allowed maximum value is %s. ""Please try again." % self.maxvalue,parent = self)return 0self.result = resultreturn 1class _QueryInteger(_QueryDialog):errormessage = "Not an integer."def getresult(self):return self.getint(self.entry.get())def askinteger(title, prompt, **kw):'''get an integer from the userArguments:title -- the dialog titleprompt -- the label text**kw -- see SimpleDialog classReturn value is an integer'''d = _QueryInteger(title, prompt, **kw)return d.resultclass _QueryFloat(_QueryDialog):errormessage = "Not a floating point value."def getresult(self):return self.getdouble(self.entry.get())def askfloat(title, prompt, **kw):'''get a float from the userArguments:title -- the dialog titleprompt -- the label text**kw -- see SimpleDialog classReturn value is a float'''d = _QueryFloat(title, prompt, **kw)return d.resultclass _QueryString(_QueryDialog):def __init__(self, *args, **kw):if "show" in kw:self.__show = kw["show"]del kw["show"]else:self.__show = None_QueryDialog.__init__(self, *args, **kw)def body(self, master):entry = _QueryDialog.body(self, master)if self.__show is not None:entry.configure(show=self.__show)return entrydef getresult(self):return self.entry.get()def askstring(title, prompt, **kw):'''get a string from the userArguments:title -- the dialog titleprompt -- the label text**kw -- see SimpleDialog classReturn value is a string'''d = _QueryString(title, prompt, **kw)return d.resultif __name__ == '__main__':def test():root = Tk()def doit(root=root):d = SimpleDialog(root,text="This is a test dialog. ""Would this have been an actual dialog, ""the buttons below would have been glowing ""in soft pink light.\n""Do you believe this?",buttons=["Yes", "No", "Cancel"],default=0,cancel=2,title="Test Dialog")print(d.go())print(askinteger("Spam", "Egg count", initialvalue=12*12))print(askfloat("Spam", "Egg weight\n(in tons)", minvalue=1,maxvalue=100))print(askstring("Spam", "Egg label"))t = Button(root, text='Test', command=doit)t.pack()q = Button(root, text='Quit', command=t.quit)q.pack()t.mainloop()test()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。