Python Forum
[Tkinter] Extrakt a Variable from a closed tkinter window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Extrakt a Variable from a closed tkinter window
#1
Hello together,

in the following code I want to start to create a tkinter window and open an Excel file, choose the sheet from a tkinter Dropdown and after pressing a Close button I want to Progress with the sheetname and the filename to create a dataframe and going for plots. However, I get enought code to create my tkinter GUI and choose the sheet, but I cant Transfer this sheetname out of the tkinter setup. I post a solution with class, but I am not sure, if ist necessary. Furthermore I am struggeling with the self command. I every case, the main Problem is usual the Output

TypeError: __init__() missing 1 required positional argument: 'master'

and I don't know how to handle this. How can I extract the tkvar right, is the self. command used right and do I have to make a class to work with tkinter as sheet selector. .... I am pretty new in Python and mainly used numpy and matplotlib at the beginning

from tkinter.filedialog import askopenfilename
import tkinter
import pandas as pd
import matplotlib.pyplot as plt

 
class App:
    def __init__(self,master):
        self.master = master
        Filename = askopenfilename(filetypes = (("ExcelFiles","*.xlsm *.xlsx *.xls"),("all files","*.*")),title='Jetzt wähl endlich die Kagg-Datei')
        print(Filename)
        
        self.xl = pd.ExcelFile(Filename)
        print(self.xl.sheet_names)
        print(self.xl.sheet_names[0])
        
        # Add a grid
        self.mainframe = tkinter.Frame(self.master)
        self.mainframe.grid(column=0,row=0, sticky=(tkinter.N,tkinter.W,tkinter.E,tkinter.S) )
        self.mainframe.columnconfigure(0, weight = 2)
        self.mainframe.rowconfigure(0, weight = 2)
        self.mainframe.pack(pady = 100, padx = 100)
        
        self.button = tkinter.Button(self.mainframe, 
                             text="Auswahl", fg="red",
                             command = master.destroy)
        
        # Create a Tkinter variable
        self.tkvar = tkinter.StringVar(self.master)
        
        #Convert xl_SheetNameList into Dictionary
        self.dictOfxl = dict.fromkeys(self.xl.sheet_names , 1)
        
        # Dictionary with options
        choices = self.dictOfxl
        self.tkvar.set(self.xl.sheet_names[0]) # set first xl_Sheet as default option
        
        self.popupMenu = tkinter.OptionMenu(self.mainframe, self.tkvar, *choices)
        tkinter.Label(self.mainframe, text="Choose a sheet").grid(row = 1, column = 1)
        self.popupMenu.grid(row = 2, column =1)
        self.button.grid(row = 3, column = 1)
        
        # on change dropdown value
        def change_dropdown(*args):
           print(self.tkvar.get() )
           
        # link function to change dropdown
        self.tkvar.trace('w', change_dropdown)


master = tkinter.Tk()
master.title("Sheetauswahl") 
app = App(master)
master.mainloop()

print(App().tkvar.get())
sheet = App().tkvar.get()
df = pd.read_excel(Filename, sheet_name=sheet)
print(df)
Best greetings
Reply


Messages In This Thread
Extrakt a Variable from a closed tkinter window - by hWp - Aug-22-2019, 11:50 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 529 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [closed] "checked" variable (attribute?) origin? paul18fr 4 542 Mar-05-2024, 04:20 PM
Last Post: deanhystad
  [Tkinter] (CLOSED) CTkScrollableDropdown error: bad window path name ".!ctkscrollabledropdown" CopperGenie 4 618 Mar-03-2024, 03:21 AM
Last Post: CopperGenie
  pass a variable between tkinter and toplevel windows janeik 10 2,366 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Tkinter multiple windows in the same window tomro91 1 861 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,864 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,920 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,934 Apr-16-2022, 04:04 PM
Last Post: DBox
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,584 Feb-17-2022, 07:02 AM
Last Post: pymn
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 2,869 Nov-07-2021, 05:14 PM
Last Post: gw1500se

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020