0

this is a follow-up from a previous question that I had posted before. The issue that I am facing is that, my pop-up menu is able to allow me to select the value that I want, however I am not able to read and use the value for subsequent processing outside the 2 Classes - App and TestFrame. As I am relatively new to Tkinter functions, I would greatly appreciate any form of help/advice on how I can read and use the values for further processing, outside the Class functions.

Thank you!

import tkinter as tk
from tkinter import ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.geometry("800x400")
        self.title('ABC')
        for r in range(5):
            self.rowconfigure(r, weight=1)
        for c in range(8):
            self.columnconfigure(c, weight=1)
        self.testFrame1 = TestFrame()
        self.testFrame1.grid(row=0, column=0, rowspan=3, columnspan=3, sticky='nsew')

class TestFrame(tk.Frame):
    def __init__(self):
        super().__init__()
        self.dimension = ('106','108','110','112','114','126','128','130','132','134','136','138','140','146','148','150','152','154','360','380','400','420','440','500', '520', '540',
                        '560', '580', '600','780','800','820')
        self.option_var =tk.IntVar()
        paddings = {'padx': 5, 'pady': 5}
        label = ttk.Label(self,  text='Select the dimension for DOB:').grid(column=0, row=0, sticky=tk.W, **paddings)
        #DOB
        option_menu = ttk.OptionMenu(
            self,
            self.option_var,
            self.dimension[0],
            *self.dimension,
            command=self.option_changed)
        option_menu.grid(column=1, row=0, sticky=tk.W, **paddings)
        self.output_label = ttk.Label(self, foreground='red')
        self.output_label.grid(column=0, row=1, sticky=tk.W, **paddings)
     def option_changed(self, *args):
        self.output_label['text'] = f'You selected: {self.option_var.get()}'
App().mainloop()
        
j_4321
  • 15,431
  • 3
  • 34
  • 61
etfl2021
  • 3
  • 3
  • Welcome. What I think is you want to access the value of ```option_var``` outside the class? –  Jun 28 '21 at 07:20
  • Hi Sujay, yes, that is right. – etfl2021 Jun 28 '21 at 07:21
  • So, instead of having to input the rest of the other codes within the Class App to access the values, is there any way that I can access the value outside the class? – etfl2021 Jun 28 '21 at 07:22
  • [Check this out](https://stackoverflow.com/questions/33646605/how-to-access-variables-from-different-classes-in-tkinter) –  Jun 28 '21 at 07:27
  • Hi Sujay, I have tried to follow something similar but still unable to work. Do you have any other tips to offer? Thank you! – etfl2021 Jun 28 '21 at 09:39
  • For your case, save instance of `App()` to a variable, e.g. `app = App()`, then use `app.testFrame1.option_var.get()` to get the selected option. – acw1668 Jun 28 '21 at 14:01
  • Hi @acw1668, I have previously tried this method, but it does not give me the selected option that I want. Instead, it gives me the default value that was set in Class TestFrame --> self.dimension[0]. Could it be that I have to link to Class TestFrame as well? – etfl2021 Jun 29 '21 at 00:58
  • If you call `app.testFrame1.option_var.get()` just after `app = App()`, then yes. You need to call it after you have selected an option. – acw1668 Jun 29 '21 at 01:03
  • Hi @acw1668, may I know what is the "it" that you are referring to in the previous comment and how should I go about it? Thank you! – etfl2021 Jun 29 '21 at 01:15
  • *It* is `app.testFram1.option_var.get()`. When to call it really depends on your application design, but as I said not just after `app = App()`. – acw1668 Jun 29 '21 at 01:18
  • Hi @acw1668, thank you for the advice and tips. Even though my code is still unable to work, I greatly appreciate your help. The issue of not being able to display my selected output even after having selected the option still exists. – etfl2021 Jun 29 '21 at 08:20

0 Answers0