我想在类之外创建一个使用一些自类变量的方法,然后在实例化时将其添加到类中。
像这样的东西:
used_method = int(input())
if used_method == 1:
def outside_method(var1=self.var1, var2=self.var2):
return (var1 + var2)
elif used_method == 2:
def outside_method(var1=self.var1, var2=self.var2, var3=self.varx):
return (var1 - var2) * var3
class myClass():
def __init__(self, varx=3, vary=2):
self.varx = varx
self.vary = vary
self.var1 = 0
self.var2 = 0
def _method1(self, x, y):
a = x + y
b = x * y
return a, b
def run(self)
self.var1, self.var2 = self._method1(self.varx, self.vary)
target = outside_method()
print(target)
Run Code Online (Sandbox Code Playgroud)
所以,当:
used_method = 1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Beeware 创建一个跨平台应用程序,一开始我显示两个按钮供用户选择他想要进入的视图,因此一旦单击按钮,主窗口应该更新其内容并显示视图用户选择的。
这是应用程序启动时的主窗口:
单击“第一个视图”后,第一个视图的内容将添加到开始内容后面,如下所示:
预期的行为是主窗口删除按钮并仅显示文本,第二个视图按钮也应该发生同样的情况。
这是代码:
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
class exampleApp(toga.App):
def startup(self):
"""
Construct and show the Toga application.
Usually, you would add your application to a main content box.
We then create a main window (with a name matching the app), and
show the main window.
"""
main_box = toga.Box(style=Pack(direction=COLUMN))
###
# Main Screen
first_view = toga.Button('First View', on_press=self.first_view, style=Pack(padding=2))
second_view = toga.Button('Second View', on_press=self.second_view, style=Pack(padding=2))
home_box = toga.Box(style=Pack(direction=ROW, …Run Code Online (Sandbox Code Playgroud)