python-3.x - 删除超类的部分方法
问题描述
我目前正在学习继承和覆盖方法。我能够添加到某个方法,但现在我正在努力从超类的方法中删除一些东西。更具体地说,我的超类是来自 tkinter 的 simpledialog 的 Dialog 类。我想更改子类中的 buttonbox() 方法,以便隐藏或删除取消按钮。我将展示 Dialog 超类的“ init ”方法和 buttonbox 方法:
class Dialog(Toplevel):
def __init__(self, parent, title = None):
Toplevel.__init__(self, parent)
self.withdraw()
if parent.winfo_viewable():
self.transient(parent)
if title:
self.title(title)
self.parent = parent
self.result = None
body = Frame(self)
self.initial_focus = self.body(body)
body.pack(padx=5, pady=5)
self.buttonbox()
if not self.initial_focus:
self.initial_focus = self
self.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 now
self.initial_focus.focus_set()
self.wait_visibility()
self.grab_set()
self.wait_window(self)
def buttonbox(self):
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()
所以现在在创建我的子类时,我想继承超类但重写按钮框方法,这样我就只有一个按钮(带有 OK 的按钮)。
MyDialogClass(simpledialog.Dialog):
def buttonbox(self):
#code that will override the buttonbox method of superclass
我如何覆盖这个?
解决方案
Welp,我试过了,我想我有一个解决方案,我现在觉得有点愚蠢。
MyDialogClass(simpledialog.Dialog):
def buttonbox(self):
box = tkinter.Frame(self)
w = tkinter.Button(box, text="OK", width=10, command=self.ok, default="active")
w.pack(side="left", padx=5, pady=5)
self.bind("<Return>", self.ok)
box.pack()
一开始我的主要问题是,我在使用“box = Frame(self)”时遇到了 NameError,就像 Superclass buttonbox 方法一样。现在我使用了 tkinter.Frame(self) 它工作了。
推荐阅读
- vue.js - 如何确认我使用的是 Vue 的“完整版本”?
- microsoft-graph-api - 使用 Microsoft Graph 登录的用户的图像 URL
- excel - Excel中具有搜索功能的多个IF语句
- php - 从 php 中的图像标签中过滤图像名称
- python - ImportError:没有名为“tensorflow.core”的模块
- java - 如何在存储库中合并过滤器领域结果和网络搜索
- windows - IMsRdpDeviceV2 是否记录在 MSDN 中,但在 mstscax.dll 中不可用?
- html - 创建带有模糊视频背景的标题字体填充:CSS + SVG Clipping + Video Background + Canvas
- json - JQ 如何将多个对象合并为一个
- android - 我们如何在不丢失 ExpandableListview 视图项的位置的情况下立即使用更新列表 API 更新 ExpandableListview?