Python class variable utilization question.

Asked 2 years ago, Updated 2 years ago, 87 views

You are creating a program in Python Tkinter. I would like to inherit the value entered in Tkinter entry from another class and use it. Help me.

import tkinter

from tkinter import *

from tkinter import filedialog

class Day_Simple:

    st_day = ""
    en_day = ""
    city_num = ""
    def __init__(self):
        self.day_simple = Tk()
        self.day_simple.title ("test")
        self.day_simple.geometry("600x400+400-500")
        self.day_simple.resizable(False, False) # Fix size

        self.search_btn = tkinter.Button(self.day_simple, text = "search", width = 10, height = 2, command = self.btn_search)
        self.search_btn.place(relx = 0.4, rely = 0.6)

        self.label_start_day = tkinter.Label(self.day_simple, text = "start date: (YYYYMMDD)", width = 20)
        self.label_start_day.place(relx = 0.15, rely = 0.1)
        self.entry_start_day = tkinter.Entry(self.day_simple, width = 20)
        self.entry_start_day.place(relx = 0.4, rely = 0.1)
        self.label_end_day = tkinter.Label(self.day_simple, text="end date: (YYYYMMDD)", width=20)
        self.label_end_day.place(relx=0.15, rely=0.2)
        self.entry_end_day = tkinter.Entry(self.day_simple, width=20)
        self.entry_end_day.place(relx=0.4, rely=0.2)
        self.label_city_num = tkinter.Label (self.day_simple, text="area code", width=20)
        self.label_city_num.place(relx=0.15, rely=0.3)
        self.entry_city_num = tkinter.Entry(self.day_simple, width=20)
        self.entry_city_num.place(relx=0.4, rely=0.3)

        self.test = tkinter.Button(self.day_simple, text = "test" ,width = 10)
        self.test.place(relx = 0.5, rely = 0.6)

        self.day_simple.mainloop()

    def close (self): # Close window
        self.day_simple.quit()
        self.day_simple.destroy()

    def btn_search(self): # Search button
        Day_Simple.st_day = self.entry_start_day.get()
        Day_Simple.en_day = self.entry_end_day.get()
        Day_Simple.city_num = self.entry_city_num.get()
        returnDay_Simple.st_day, Day_Simple.en_day, Day_Simple.city_num #returnclass variable

I want to take the returned value from another class and use it as it is.

I would like to know if you put the entry value in st_day, en_day, and city_num and use it in other classes.

python tkinter

2022-09-20 15:43

1 Answers

Once class is declared, the class variable in that class can be accessed anywhere.

print(Day_Simple.st_day)

You can also declare a class method to obtain a value.

@classmethod
def getClassValue(cls):
return Day_Simple.st_day, Day_Simple.en_day, Day_Simple.city_num


2022-09-20 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.