[tkinter(i think)] How to organize my GUI

Post here if you need help with creating a Graphical User Interface in Python.

[tkinter(i think)] How to organize my GUI

Postby Nikmo Sarata » Sat Jun 15, 2013 10:57 pm

Hello,
i just started with python because i want to do a program and i dont have
pretty much time to read, so i wanted to ask to the comunity to win time.

So my question is, how is the architure of a program like this?
Multiple WIndows.jpg
Multiple WIndows.jpg (20.61 KiB) Viewed 566 times



This are the different kind of solutions of what i've think.

- make a big canvas on a window and change the content of the canvas to "pass" from the Main interface to go to a another interface..
- make a window for each interface and the buttons will close them and open them.

so i tryed to do the second solution.. and i tryed it with "def()", so the program of each interface was inside of a definition and then in the main window i called the definition. the problems is that
i couln'd do that the button close the main interface( with: root.destroy), and then, i wasn't able to go back to the main interface.

so im kinda lost and i'd like to have any clue haha.
also i'd like to know if it's a good a idea to build the interfaces separetly and then when they be finish assamble them together??

Thanks Alot!!
Last edited by Yoriz on Sun Jun 16, 2013 1:59 pm, edited 1 time in total.
Reason: Editted the title to include more information.
Nikmo Sarata
 
Posts: 2
Joined: Sat Jun 15, 2013 10:21 pm

Re: [tkinter(i think)] How to organize my GUI

Postby wuf » Mon Jun 17, 2013 10:45 am

Hi Nikmo Sarata

Wellcome in our forum.
Here one way to a solution. Tested on Windows 8:
Code: Select all
#!/usr/bin/env python
# coding: UTF-8

try:
    #~~ For Python 2.x
    import Tkinter as tk
except ImportError:
    #~~ For Python 3.x
    import tkinter as tk

MAIN_WIN_XPOS = 50
MAIN_WIN_YPOS = 50
MAIN_WIN_WIDTH = 300
MAIN_WIN_HEIGHT = 100
MAIN_WIN_TITLE = "Main Interface"
MAIN_BACK_GND = 'palegoldenrod'

INTERFACE_XORG = 400
INTERFACE_YORG = MAIN_WIN_YPOS
INTERFACE_XYOFFSET = 30
INTERFACE_WIDTH = 250
INTERFACE_HEIGHT = 100
NUM_OF_INTERFACES = 8

class MyApp(tk.Tk):
   
    def __init__(self, app_title="My application"):
       
        tk.Tk.__init__(self)
        self.geometry('{0}x{1}+{2}+{3}'.format(
            MAIN_WIN_WIDTH, MAIN_WIN_HEIGHT, MAIN_WIN_XPOS, MAIN_WIN_YPOS))
        self.protocol("WM_DELETE_WINDOW", self.close)
        self.title(app_title)
        self.config(bg=MAIN_BACK_GND)
       
        self.interface_windows = list()

        tk.Button(self, text='Add new interface window',
            command=self.create_interface_win).pack(expand=True)

        # Create 5 interface windows
        self.iterface_win_xpos = INTERFACE_XORG
        self.iterface_win_ypos = INTERFACE_YORG

        for interface_win in range(NUM_OF_INTERFACES):
            self.create_interface_win()
         
    def create_interface_win(self):
        interface_win = tk.Toplevel()
        interface_win.title('Interface-{0}'.format(len(self.interface_windows)))
        interface_win.geometry('{0}x{1}+{2}+{3}'.format(
            INTERFACE_WIDTH, INTERFACE_HEIGHT, self.iterface_win_xpos,
            self.iterface_win_ypos))
        interface_win.focus_set()
        interface_win.update_idletasks()

        self.interface_windows.append(interface_win)
        self.iterface_win_xpos += INTERFACE_XYOFFSET
        self.iterface_win_ypos += INTERFACE_XYOFFSET
   
    def close(self):
        """Do something before application shutdown"""
        print ("Shut down application")
        self.destroy()
       
    def run(self):
        self.mainloop()
       
app = MyApp(MAIN_WIN_TITLE)
app.run()

wuf ;)
wuf
 
Posts: 37
Joined: Fri Feb 08, 2013 6:42 am

Re: [tkinter(i think)] How to organize my GUI

Postby Nikmo Sarata » Sat Sep 14, 2013 9:15 pm

Thanks alot wulf :)
Nikmo Sarata
 
Posts: 2
Joined: Sat Jun 15, 2013 10:21 pm


Return to GUI

Who is online

Users browsing this forum: Google [Bot] and 1 guest