Search code examples
pythonuser-interfacetkinterpython-importoptionmenu

Load variable from script


I know it is a common question, but I can't find a solution which is similar with mine. I have two program files - a GUI-File (tkinter) and the Main-File, which is feeding the Gui with input.

It works fine until I want to put an OptionMenu into the GUI-File, which is getting the List from the Main-File.

I know that's normally works with:

from GUI-File import variable_from_Main-File

BUT! When I do this the whole file is loaded before the GUI is finished, so variables are just undefined on some points than.

Here is the code for better understanding:

1st the Main-File

import xml.etree.ElementTree as ET
import DLE_Gui as gui

xmlOriginUp = open('%s' %gui.load.Filename, 'r')
xmlOrigin = ET.parse(xmlOriginUp)

elemList    = []
tags        = []

for elem in xmlOrigin.iter():
    elemList.append(elem)

for ta in range(len(elemList)):
    tags.append(elemList[ta].tag)

And the GUI-File:

import tkinter 
from tkinter import filedialog, Tk, Button, LabelFrame, Label, Entry, messagebox, Text, OptionMenu, StringVar
from tkinter.filedialog import askopenfilename
from DLE_v200609 import tags

def fin():
    gui.destroy()

def load():        
    load.Filename = askopenfilename(filetypes=(("XML", "*.xml"),("All files", "*.*")))
    print(load.Filename)    
    Label(text=load.Filename, bg="white", borderwidth=2, relief="sunken").grid(row=1, column=1)


gui = tkinter.Tk()
gui.title('Dante Label XML-Export')


Label(text='Öffnen:').grid(row=1, sticky='w', padx=5, pady=5)
Label(bg="white", borderwidth=2, relief="sunken").grid(row=1, column=1, ipadx=178)
Button(text='dursuchen...', command=load).grid(row=1, column=2, ipadx=5)

Label(text='Label:').grid(row=2, sticky='w', padx=5, pady=5)
var = StringVar(gui)
var.set('---')
optMen = OptionMenu(gui, var, tags)
optMen.grid(row=2, column=1)

Button(text='Schließen', command=fin).grid(row=4, column=2, padx=10, pady=10)

gui.mainloop()

So I want the list 'tags[]' as variable list of the Option Menu in the GUI. The answer of the program is:

xmlOriginUp = open('%s' %gui.load.Filename, 'r')

AttributeError: module 'DLE_Gui' has no attribute 'load'

Has anybody an idea how can I solve it, please?


Solution

  • All your idea with two files seems wrong.

    You could put all code in one file to make it simpler.


    To create tags you have to read file but to get filename you have to display GUI with button which runs load() - but at the same time you try to get filename without displaying GUI and without running load().

    You should create GUI with empty OptionMenu as start

     option_menu = tk.OptionMenu(gui, var, '---')
    

    to display it before you load data from file for this OptionMenu

    And when you press Button then load() should read file and add items to OptionMenu

    for item in xmlOrigin.iter():
        option_menu['menu'].add_command(label=item.tag)
    

    Minimal working code

    import tkinter as tk
    from tkinter.filedialog import askopenfilename
    import xml.etree.ElementTree as ET
    
    # --- functions ---
    
    def load():
        filename = askopenfilename(filetypes=(("XML", "*.xml"),("All files", "*.*")))
        print(filename)
    
        label_filename['text'] = filename
    
        xmlOriginUp = open(filename)
        xmlOrigin = ET.parse(xmlOriginUp)
    
        for item in xmlOrigin.iter():
            option_menu['menu'].add_command(label=item.tag)
    
    # --- main ---
    
    gui = tk.Tk()
    gui.title('Dante Label XML-Export')
    
    l = tk.Label(text='Öffnen:')
    l.grid(row=1, sticky='w', padx=5, pady=5)
    
    label_filename = tk.Label(bg="white", borderwidth=2, relief="sunken")
    label_filename.grid(row=1, column=1, ipadx=178)
    
    b = tk.Button(text='dursuchen...', command=load)
    b.grid(row=1, column=2, ipadx=5)
    
    l = tk.Label(text='Label:')
    l.grid(row=2, sticky='w', padx=5, pady=5)
    
    option_menu_var = tk.StringVar(gui)
    option_menu_var.set('---')
    
    option_menu = tk.OptionMenu(gui, option_menu_var, '---') # create empty OptionMenu
    option_menu.grid(row=2, column=1)
    
    b = tk.Button(text='Schließen', command=gui.destroy)
    b.grid(row=4, column=2, padx=10, pady=10)
    
    gui.mainloop()