Home > Enterprise >  How to access variable from tkinter class
How to access variable from tkinter class

Time:08-08

I have built a simple one-page application using tkinter, whose script is executed when the program is started. A variable called self.index is created at the same time, which contains a certain number. I need to import this number in another script of the program, but this turned out to be tricky. Here is my tkinter class:

class ChatApp:

    def __init__(self):
        self.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)
        self.index = db_getid["id"][0]
        connection.commit()
        connection.close()

    def _setup_main_window(self):
        f = open('files/'   str(self.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(self.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()

I first tried importing the entire tkinter class and then using

app = ChatApp() 
index = app.index 

to get the value of the index. That actually works so far. But this causes the interface to be opened a second time. So this short code section causes a complete call of the class ChatApp, which I want to avoid.

CodePudding user response:

If in each instance the index is the same you can use a static value like this

ChatApp.index = <some value>

And when you need it

index = ChatApp.index

CodePudding user response:

Why not making the file where you need the index value the start file and creating an object there?

  • Related