![](/img/trans.png)
[英]How to access a method variable (Tkinter) from another method in same class?
[英]How to access variable from tkinter class
我使用 tkinter 構建了一個簡單的單頁應用程序,它的腳本在程序啟動時執行。 同時創建了一個名為 self.index 的變量,其中包含一定的數字。 我需要在程序的另一個腳本中導入這個數字,但這很棘手。 這是我的 tkinter class:
class ChatApp:
index = 0
def __init__(self):
ChatApp.index = 0
self.create_index()
self.window = Tk()
self._setup_main_window()
def run(self):
self.window.mainloop()
def create_index(self):
connection = sqlite3.connect("database.db")
cur = connection.cursor()
cur.execute("INSERT INTO chats (content) VALUES(Null)")
db_getid = pd.read_sql('select id from chats order by id desc limit 1', connection)
ChatApp.index = db_getid["id"][0]
connection.commit()
connection.close()
def _setup_main_window(self):
f = open('files/' + str(ChatApp.index) + ".txt", "w+")
self.window.title("Chat")
self.window.resizable(width=False, height=False)
self.window.configure(width=500, height=600, bg=background_color)
head_label = Label(self.window, bg=background_color,
fg=text_color, text="#"+str(ChatApp.index), font="font_bold", pady=10)
head_label.place(relwidth=1)
line = Label(self.window, width=480, bg=background_gray)
line.place(relwidth=1, rely=0.07, relheight=0.012)
self.text_widget = Text(self.window, width=20, height=2, bg=background_color, fg=text_color,
font=font, padx=5, pady=5)
self.text_widget.place(relheight=0.745, relwidth=1, rely=0.08)
self.text_widget.configure(cursor="arrow", state=DISABLED)
scrollbar = Scrollbar(self.text_widget)
scrollbar.place(relheight=1, relx=0.974)
scrollbar.configure(command=self.text_widget.yview)
bottom_label = Label(self.window, bg=background_gray, height=80)
bottom_label.place(relwidth=1, rely=0.825)
self.msg_entry = Entry(bottom_label, bg='#2C3E50', fg=text_color, font=font)
self.msg_entry.place(relwidth=0.74, relheight=0.06, rely=0.008, relx=0.011)
self.msg_entry.focus()
self.msg_entry.bind("<Return>", self._on_enter_pressed)
send_button = Button(bottom_label, text="Send", font=font_bold, width=20, bg=background_gray,
command=lambda: self._on_enter_pressed(None))
send_button.place(relx=0.77, rely=0.008, relheight=0.06, relwidth=0.22)
def _on_enter_pressed(self, event):
msg = self.msg_entry.get()
self._insert_message(msg, "You")
def _insert_message(self, msg, sender):
if not msg:
return
self.msg_entry.delete(0, END)
msg1 = f"{sender}: {msg}\n\n"
self.text_widget.configure(state=NORMAL)
self.text_widget.insert(END, msg1)
self.text_widget.configure(state=DISABLED)
msg2 = f"bot: {chat(msg)}\n\n"
self.text_widget.configure(state=NORMAL)
self.text_widget.insert(END, msg2)
self.text_widget.configure(state=DISABLED)
self.text_widget.see(END)
if __name__ == "__main__":
app = ChatApp()
app.run()
我首先嘗試導入整個 tkinter class 然后使用
index = ChatApp.index
獲取索引的值。 到目前為止,這實際上有效。 但這會導致第二次打開界面。 所以這個簡短的代碼部分會導致 class ChatApp 的完整調用,我想避免這種情況。
如果在每個實例中索引相同,您可以使用 static 值代替app.index
ChatApp.index = <some value>
當你需要它時
index = ChatApp.index
為什么不將需要索引值的文件作為起始文件並在那里創建 object?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.