如何解决无法访问 Typescript 方法中的实例变量 |它说变量无法访问
import tkinter as tk
LARGE_FONT_STYLE = ("Arial",40,"bold")
SMALL_FONT_STYLE = ("Arial",16)
DIGITS_FONT_STYLE = ("Arial",25,"bold")
DEFAULT_FONT_STYLE = ("Arial",20)
OFF_WHITE = "#F8FAFF"
WHITE = "#FFFFFF"
LIGHT_BLUE = "#CCDDFF"
LIGHT_GRAY = "#F5F5F5"
LABEL_COLOR = "#25265E"
class Calculator:
def __init__(self):
self.window = tk.Tk()
self.window.geometry("375x667")
self.window.resizable(0,0)
self.window.title("Calculator")
self.total_expression = "0"
self.current_expression = "0"
self.display_frame = self.create_display_frame()
self.total_label,self.label = self.create_display_labels()
self.digits = {
7: (1,1),8: (1,2),9: (1,3),4: (2,5: (2,6: (2,1: (3,2: (3,3: (3,0: (4,".": (4,1)
}
self.operations = {"/": "\u00F7","*": "\u00D7","-": "-","+": "+"}
self.buttons_frame = self.create_buttons_frame()
self.buttons_frame.rowconfigure(0,weight=1)
for x in range(1,5):
self.buttons_frame.rowconfigure(x,weight=1)
self.buttons_frame.columnconfigure(x,weight=1)
self.create_digit_button()
self.create_operator_button()
self.create_special_button()
def create_special_button(self):
self.create_clear_button()
self.create_equals_button()
def create_display_labels(self):
total_label = tk.Label(self.display_frame,text=self.total_expression,anchor=tk.E,bg=LIGHT_GRAY,fg=LABEL_COLOR,padx=24,font=SMALL_FONT_STYLE)
total_label.pack(expand=True,fill="both")
label = tk.Label(self.display_frame,text=self.current_expression,font=LARGE_FONT_STYLE)
label.pack(expand=True,fill="both")
return total_label,label
def create_display_frame(self):
frame = tk.Frame(self.window,height=221,bg=LIGHT_GRAY)
frame.pack(expand=True,fill="both")
return frame
def create_digit_button(self):
for digit,grid_value in self.digits.items():
button = tk.Button(self.buttons_frame,text=str(digit),bg=WHITE,font=DIGITS_FONT_STYLE,borderwidth=0)
button.grid(row=grid_value[0],column=grid_value[1],sticky=tk.NSEW)
def create_operator_button(self):
i = 0
for operator,symbol in self.operations.items():
button = tk.Button(self.buttons_frame,text=symbol,bg=OFF_WHITE,font=DEFAULT_FONT_STYLE,borderwidth=0)
button.grid(row=i,column=4,sticky=tk.NSEW)
i += 1
def create_clear_button(self):
button = tk.Button(self.buttons_frame,text="C",borderwidth=0)
button.grid(row=0,column=1,columnspan=3,sticky=tk.NSEW)
def create_equals_button(self):
button = tk.Button(self.buttons_frame,text="+",bg=LIGHT_BLUE,borderwidth=0)
button.grid(row=4,column=3,columnspan=2,sticky=tk.NSEW)
def create_buttons_frame(self):
frame = tk.Frame(self.window)
frame.pack(expand=True,fill="both")
return frame
def run(self):
self.window.mainloop()
if __name__ == "__main__":
calc = Calculator()
calc.run()
当我尝试将方法中的布尔值赋值为 true 时,会给出以下消息
** 有人可以解释发生错误的原因以及全局变量如何在 Typescript / Javascript 中的类中与其他语言一样工作,这不会成为问题
解决方法
这里的问题似乎很简单,您在 new Promise
方法中返回了一个 Token_Valid()
,但在返回后设置了 this.token_status
。
在 JavaScript 中,在方法 * 的 return
语句之后不能执行任何操作,因此您需要将该方法重构为:
- 在返回承诺之前设置变量,
- 通过等待将 promise 的值保存为变量,然后设置
this.token_status
,并返回之前保存的值。 - 向返回的承诺添加一个
.then()
,该承诺接收一个值(从创建的承诺返回的值),该值更改this.token_status
的值,然后返回它接收的值。
*除非您使用 try { ... } finally { ... }
块。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。