我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用tkinter.messagebox.askyesnocancel()。 def maybesave(self): if self.get_saved(): return "yes" message = "Do you want to save %s before closing?" % ( self.filename or "this untitled document") confirm = tkMessageBox.askyesnocancel( title="Save On Close", message=message, default=tkMessageBox.YES, master=self.text) if confirm: reply = "yes" self.save(None) if not self.get_saved(): reply = "cancel" elif confirm is None: reply = "cancel" else: reply = "no" self.text.focus_set() return reply def main(): # ?????????,?????Tkinter????????Tk??.??????withdraw()?????? tk = tkinter.Tk() tk.withdraw() # ????? print(dir(mb)) # ??,?????????,??ok,????????????.??????????????,??????????. # ??,???Cancel?,??????None mb.showinfo("Title", "Your message here") mb.showerror("An Error", "Oops!") mb.showwarning("Title", "This may not work...") mb.askyesno("Title", "Do you love me?") mb.askokcancel("Title", "Are you well?") mb.askquestion("Title", "How are you?") mb.askretrycancel("Title", "Go again?") mb.askyesnocancel("Title", "Are you well?") def maybesave(self): if self.get_saved(): return "yes" message = "Do you want to save %s before closing?" % ( self.filename or "this untitled document") confirm = tkMessageBox.askyesnocancel( title="Save On Close", message=message, default=tkMessageBox.YES, parent=self.text) if confirm: reply = "yes" self.save(None) if not self.get_saved(): reply = "cancel" elif confirm is None: reply = "cancel" else: reply = "no" self.text.focus_set() return reply def maybesave(self): if self.get_saved(): return "yes" message = "Do you want to save %s before closing?" % ( self.filename or "this untitled document") confirm = tkMessageBox.askyesnocancel( title="Save On Close", message=message, default=tkMessageBox.YES, master=self.text) if confirm: reply = "yes" self.save(None) if not self.get_saved(): reply = "cancel" elif confirm is None: reply = "cancel" else: reply = "no" self.text.focus_set() return reply def can_be_closed(self): """ This overrides :meth:`Tab.can_be_closed` in order to display a save dialog. If the file has been saved, this returns True and the tab is closed normally. Otherwise this method asks the user whether the file should be saved, and returns False only if the user cancels something (and thus wants to keep working on this file). """ if self.is_saved(): return True if self.path is None: msg = "Do you want to save your changes?" else: msg = ("Do you want to save your changes to %s?" % os.path.basename(self.path)) answer = messagebox.askyesnocancel("Close file", msg) if answer is None: # cancel return False if answer: # yes return self.save() # no was clicked, can be closed return True # TODO: document the overriding
项目:Python-GUI-Programming-Cookbook-Second-Edition
作者:PacktPublishing
文件:GUI_message_box_yes_no_cancel.py |
项目源码 |
文件源码
def _msgBox(): # msg.showinfo('Python Message Info Box', 'A Python GUI created using tkinter:\nThe year is 2017.') # msg.showwarning('Python Message Warning Box', 'A Python GUI created using tkinter:\nWarning: There might be a bug in this code.') # msg.showerror('Python Message Error Box', 'A Python GUI created using tkinter:\nError: Houston ~ we DO have a serious PROBLEM!') answer = msg.askyesnocancel("Python Message Multi Choice Box", "Are you sure you really wish to do this?") print(answer) # Add another Menu to the Menu Bar and an item def prompt_save(self, editor): fname = editor.fpathname or editor.fname msg = "Save '%s' before closing?" % fname ans = askyesnocancel(message=msg) if ans: # return cancel if selected save and then not saved return True if self.save(editor) else None return ans def main(): top = tix.Tk() nb = tix.NoteBook(top, width=300, height=200) nb.pack(expand=True, fill="both") nb.add("page1", label="text") f1 = tix.Frame(nb.subwidget("page1")) st = tix.ScrolledText(f1) st.subwidget("text").insert("1.0", "Here is where the text goes...") st.pack(expand=True) f1.pack() nb.add("page2", label="Message Boxes") f2 = tix.Frame(nb.subwidget("page2")) # ??????expand,fill?anchor??????????????????????? tix.Button(f2, text="error", bg="lightblue", command=lambda t="error", m="This is bad!": mb.showerror(t, m)).pack(fill="x", expand=True) tix.Button(f2, text="info", bg="pink", command=lambda t="info", m="Information": mb.showinfo(t, m)).pack(fill="x", expand=True) tix.Button(f2, text="warning", bg="yellow", command=lambda t="warning", m="Don't do it!": mb.showwarning(t, m)).pack(fill="x", expand=True) tix.Button(f2, text="question", bg="green", command=lambda t="question", m="Will I?": mb.askquestion(t, m)).pack(fill="x", expand=True) tix.Button(f2, text="yes - no", bg="lightgrey", command=lambda t="yes - no", m="Are you sure?": mb.askyesno(t, m)).pack( fill="x", expand=True) tix.Button(f2, text="yes - no - cancel", bg="black", fg="white", command=lambda t="yes - not - cancel", m="Last chance...": mb.askyesnocancel(t, m)).pack(fill="x", expand=True) f2.pack(side="top", fill="x") top.mainloop() def socket_listener(self, data): if data['type'] == MessageType.login_bundle: bundle = data['parameters'] friends = bundle['friends'] rooms = bundle['rooms'] messages = bundle['messages'] for friend in friends: self.handle_new_contact(friend) for room in rooms: self.handle_new_contact(room) for item in messages: # [[data:bytes,sent:int]] sent = item[1] message = _deserialize_any(item[0]) client.util.socket_listener.digest_message(message, not sent) self.bundle_process_done = True self.refresh_contacts() if data['type'] == MessageType.incoming_friend_request: result = messagebox.askyesnocancel("????", data['parameters']['nickname'] + "?????????????(?Cancel??????)"); if result == None: return self.sc.send(MessageType.resolve_friend_request, [data['parameters']['id'], result]) if data['type'] == MessageType.contact_info: self.handle_new_contact(data['parameters']) return if data['type'] == MessageType.add_friend_result: if data['parameters'][0]: messagebox.showinfo('????', '???????') else: messagebox.showerror('??????', data['parameters'][1]) return if data['type'] == MessageType.friend_on_off_line: friend_user_id = data['parameters'][1] for i in range(0, len(self.contacts)): if self.contacts[i]['id'] == friend_user_id and self.contacts[i]['type'] == 0: self.contacts[i]['online'] = data['parameters'][0] break self.refresh_contacts() return |
|
来自: ooAAMM > 《11d Python》