【发布时间】:2022-06-11 03:18:29
【问题描述】:
这是一个窗口的代码,使用tkinter 库和OOP。我想将App 类的方法设为私有。但是其中一些,比如下面代码中的方法destroy应该是公开的
from tkinter import *
from tkinter import ttk
class App(Tk):
def __init__(self):
super().__init__()
# window settings
root.title("Private Attributes")
self.resizable(width=False, height=False)
root = App() # create window
root.title("Public Attributes") # this shouldn't work
ttk.Label(root, text="Close this window").pack()
ttk.Button(root, text="Close", command=root.destroy).pack() # this should work
root.mainloop()
【问题讨论】:
-
你不能,至少在不违反里氏替换原则的情况下不能。作为
Tk的实例(通过子类App),必须假定root具有title方法。
标签: python python-3.x class oop tkinter