I have been working on this for a while. I'm new to python programming. I've been designing a user interface. I origionally created a python file for each interface. Such as the two below.

mainmenu.py

from Tkinter import *

class mainMenu:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.mbar = Frame(frame, relief = 'raised', bd=2)
        self.mbar.pack(fill = X)

        # Create File menu
        self.filebutton = Menubutton(self.mbar, text = 'File')
        self.filebutton.pack(side = LEFT)

        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu

        # Populate File menu
        self.filemenu.add('command', label = 'Exit', command = self.quit)

        # Message
        iframen = Frame(frame, bd=2, relief=FLAT)
        Message(iframen, text='Choose Either All Courses, General Education or Specific Degrees to get started.', width=700,
                relief=SUNKEN).pack(fill=X, padx=5)
        iframen.pack(expand=1, fill=X, pady=10, padx=5)


        # Buttons
        iframe1 = Frame(frame, bd=2, relief=FLAT)
        Button(iframe1, text='All Courses').pack(side=LEFT, padx=5)
        Button(iframe1, text='General Education').pack(side=LEFT, padx=5)
        Button(iframe1, text='Specific Degrees').pack(side=LEFT, padx=5)
        v=IntVar()
        iframe1.pack(expand=1, fill=X, pady=10, padx=5)

    def quit(self):
        root.destroy()

    def stub(self):
        pass

root = Tk()
root.option_add('*font', ('verdana', 8, 'bold'))
all = mainMenu(root)
root.title('Main Menu')
root.mainloop()

allcourses.py

from Tkinter import *

class AllCourses:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.mbar = Frame(frame, relief = 'raised', bd=2)
        self.mbar.pack(fill = X)

        # Create File menu
        self.filebutton = Menubutton(self.mbar, text = 'File')
        self.filebutton.pack(side = LEFT)

        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu

        # Populate File menu
        self.filemenu.add('command', label = 'Exit', command = self.quit)

        iframen = Frame(frame, bd=2, relief=FLAT)
        Message(iframen, text='Enter Courses Taken', width=300,
                    relief=SUNKEN).pack(fill=X, padx=5)
        iframen.pack(expand=1, fill=X, pady=10, padx=5)

        # Course Entries
        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='Student Id:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(iframe2, textvariable=t, bg='white').pack(side=LEFT, padx=5)
        t.set('')
        iframe2.pack(expand=1, fill=X, pady=10, padx=5)

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='Course Name:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(iframe2, textvariable=t, bg='white').pack(side=LEFT, padx=5)
        t.set('')
        iframe2.pack(expand=1, fill=X, pady=10, padx=5)

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='Course Number:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(iframe2, textvariable=t, bg='white').pack(side=LEFT, padx=5)
        t.set('')
        iframe2.pack(expand=1, fill=X, pady=10, padx=5)

        #Buttons
        iframe1 = Frame(frame, bd=2, relief=FLAT)
        Button(iframe1, text='Add Another Course Taken').pack(side=LEFT, padx=5)
        Button(iframe1, text='Enter Course To Take').pack(side=LEFT, padx=5)
        Button(iframe1, text='Main Menu').pack(side=LEFT, padx=5)
        v=IntVar()
        iframe1.pack(expand=1, fill=X, pady=10, padx=5)



    def quit(self):
        root.destroy()

    def stub(self):
        pass

root = Tk()
root.option_add('*font', ('verdana', 10, 'bold'))
all = AllCourses(root)
root.title('All Courses - Entry')
root.mainloop()

With this method I couldn't figure out how to get the menu buttons to open the other python files. I was given the suggestion to put it all in the same file and use commands to clear out the current window and replace it with the new window. I attempted to figure that out in the code below.

mainmenutest.py

from Tkinter import *

class MainMenu:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.mbar = Frame(frame, relief = 'raised', bd=2)
        self.mbar.pack(fill = X)

        # Create File menu
        self.filebutton = Menubutton(self.mbar, text = 'File')
        self.filebutton.pack(side = LEFT)

        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu

        # Populate File menu
        self.filemenu.add('command', label = 'Exit', command = self.quit)

        # Message
        iframen = Frame(frame, bd=2, relief=FLAT)
        Message(iframen, text='Choose Either All Courses, General Education or Specific Degrees to get started.', width=700,
                relief=SUNKEN).pack(fill=X, padx=5)
        iframen.pack(expand=1, fill=X, pady=10, padx=5)


        # Buttons
        iframe1 = Frame(frame, bd=2, relief=FLAT)
        Button(iframe1, text='All Courses', command = self.AllCourses).pack(side=LEFT, padx=5)
        Button(iframe1, text='General Education').pack(side=LEFT, padx=5)
        Button(iframe1, text='Specific Degrees').pack(side=LEFT, padx=5)
        v=IntVar()
        iframe1.pack(expand=1, fill=X, pady=10, padx=5)

    def quit(self):
        root.destroy()

    def stub(self):
        pass

class AllCourses:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.mbar = Frame(frame, relief = 'raised', bd=2)
        self.mbar.pack(fill = X)

        # Create File menu
        self.filebutton = Menubutton(self.mbar, text = 'File')
        self.filebutton.pack(side = LEFT)

        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu

        # Populate File menu
        self.filemenu.add('command', label = 'Exit', command = self.quit)

        iframen = Frame(frame, bd=2, relief=FLAT)
        Message(iframen, text='Enter Courses Taken', width=300,
                    relief=SUNKEN).pack(fill=X, padx=5)
        iframen.pack(expand=1, fill=X, pady=10, padx=5)

        # Course Entries
        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='Student Id:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(iframe2, textvariable=t, bg='white').pack(side=LEFT, padx=5)
        t.set('')
        iframe2.pack(expand=1, fill=X, pady=10, padx=5)

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='Course Name:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(iframe2, textvariable=t, bg='white').pack(side=LEFT, padx=5)
        t.set('')
        iframe2.pack(expand=1, fill=X, pady=10, padx=5)

        iframe2 = Frame(frame, bd=2, relief=RIDGE)
        Label(iframe2, text='Course Number:').pack(side=LEFT, padx=5)
        t = StringVar()
        Entry(iframe2, textvariable=t, bg='white').pack(side=LEFT, padx=5)
        t.set('')
        iframe2.pack(expand=1, fill=X, pady=10, padx=5)

        #Buttons
        iframe1 = Frame(frame, bd=2, relief=FLAT)
        Button(iframe1, text='Add Another Course Taken').pack(side=LEFT, padx=5)
        Button(iframe1, text='Enter Course To Take').pack(side=LEFT, padx=5)
        Button(iframe1, text='Main Menu').pack(side=LEFT, padx=5)
        v=IntVar()
        iframe1.pack(expand=1, fill=X, pady=10, padx=5)



    def quit(self):
        root.destroy()

    def stub(self):
        pass


root = Tk()
root.option_add('*font', ('verdana', 8, 'bold'))
all = MainMenu(root)
root.title('Main Menu')
root.mainloop()

I couldn't figure out how to make this work, so I gave it antoher change the code into another way.

test.py

from Tkinter import *

class Application(Frame):
    def allCourses(self):
        self.filebutton = Menubutton(self, text = 'File')
        self.filebutton.pack(side = LEFT)
        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu
        self.filemenu.add('command', label = 'Exit', command = self.quit)

        iframen = Frame(self)
        self.messageBlock = Message(self)
        self.messageBlock["text"] = "Enter Courses Taken"
        self.messageBlock.pack({"side": "bottom"})

        self.label1 = Label(self)
        self.label1["text"] = "Student ID:"
        self.label1.pack({"side": "left"})

        t = StringVar()
        self.entry1 = Entry(self)
        self.entry1["textvarriable"] = t
        self.entry1["bg"] = "white"
        self.entry1.pack({"side": "left"})
        t.set('')

    def mainMenu(self):
        self.filebutton = Menubutton(self, text = 'File')
        self.filebutton.pack(side = LEFT)
        self.filemenu = Menu(self.filebutton, tearoff=0)
        self.filebutton['menu'] = self.filemenu
        self.filemenu.add('command', label = 'Exit', command = self.quit)

        iframen = Frame(self)
        self.messageBlock = Message(self)
        self.messageBlock["text"] = "Choose Either All Courses, General Education or Specific Degrees."
        self.messageBlock.pack({"side": "bottom"})


        self.allCourses = Button(self)
        self.allCourses["text"] = "All Courses"
        self.allCourses["command"] = self.allCourses
        self.allCourses.pack({"side": "left"})

        self.generalEdu = Button(self)
        self.generalEdu["text"] = "General Education"
        self.generalEdu["command"] = self.quit
        self.generalEdu.pack({"side": "left"})

        self.specificDegree = Button(self)
        self.specificDegree["text"] = "Specific Degrees"
        self.specificDegree["command"] = self.quit
        self.specificDegree.pack({"side": "left"})

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.mainMenu()

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

I couldn't figure outshow the message box on the mainMenu one in this file.

I am hoping someone here will be able to suggest something else. I don't think I have enough knowledge of python to figure this out on my own. I want to either figure out how to open the individual python files when I click on a button, or figure out how to switch between the classes or definitions. If I could clear out the previous window when the new one is open, I would rather be able to do that.

Any help would be greatly appreciated!

Recommended Answers

All 3 Replies

Look. I honestly can't make it through all that code but I can say that I used TK to roll my own GUI's and always found eventually the GUI code would end up longer than the source to begin with. I just posted this tutorial on a package that will make your life mega easier for GUI's in python if you don't want to reinvent every wheel ever. You can skip to about 5-10 min in to part 1 and you'll see I make a GUI with an 11 line code that has buttons and everything.

http://www.daniweb.com/software-development/python/threads/419559/application-programming-tutorial-using-enthought-tool-suite

I want to either figure out how to open the individual python files when I click on a button

That is too much code to go through, but you would include the "open the file" code in the call back function.

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x
#
def open_file():
    fp = open("./test_file", "r")
    for rec in fp:
        print rec
    fp.close()
#
## create a test file to open
fp = open("./test_file", "w")
fp.write("This is a test file\n")
fp.write("Second line")
fp.close()
#
root = tk.Tk()
tk.Button(root, text="Open a File", command=open_file).grid()
root.mainloop()
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.