Hi I am trying my hand at GUI using Python and Tkinter. I know there are probably better options out there but I only have access to Tkinter here at work. My question is this. I have written this:

from Tkinter import *
import os
root = Tk()

root.title("EIMA validation tool")

var = StringVar()
tdir = os.getcwd()

files_and_dirs = [file for file in os.listdir(tdir) if os.path.isdir(file)]

the_menu = OptionMenu(root, var, *files_and_dirs)
the_menu.grid(row = 0, column=0)


At the moment this doesn't do much but at least it's starting to look like something. In this case files_and_dirs is a list of directories which is used to populate my OptionMenu this works great. But now I'm trying to turn this into a "proper" script :

from Tkinter import *
import ttk
import os

class IEMA(object):
    def __init__(self, m_window):
        m_window.title('IEMA validation app')

        self.left_frame = ttk.Frame(m_window)

        self.var = StringVar()
        self.tdir = os.getcwd()
        self.files_and_dirs = ListType()
        self.files_and_dirs = [file for file in os.listdir(self.tdir) if os.path.isdir(file) or file.endswith(('xml', 'XML'))]
        self.the_menu = OptionMenu(self.left_frame, self.var, self.files_and_dirs)
        self.the_menu.grid(row=0, column=0)

if __name__ == '__main__':
    root = Tk()
    iema_app = IEMA(root)

This, *files_and_dirs , results in an Invalid Syntax error and just using self.files_and_dirs does not work. So far Google hasn't helped. Does anyone know how to fix this?

Recommended Answers

All 5 Replies

You mean *self.files_and_dirs?

Ha ha.. actually that's not even funny... Thank you Mr. Moderator!

Nice clean start!

Just a note. As your program gets larger, using
from Tkinter import *
can lead to difficult to trace namespace conflicts. It's better to use a namespace like
import Tkinter as tk

Ufortunately I had to junk this project. I quite liked using Tkinter and would have loved to use it more but I can;t use it at work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.