Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6697087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:25:05+00:00 2026-05-26T06:25:05+00:00

Hopefully this doesn’t fall under general discussion topic, since I’d like it to be

  • 0

Hopefully this doesn’t fall under “general discussion topic”, since I’d like it to be more about resolving these issues in an efficient manner than a giant debate about which general approach to GUI programming is the absolute best.

So I’ve started some GUI programming with tkinter and long story short my code is getting pretty ugly pretty quickly. I’m trying to create a tile-based map editor for a video game. My main issues seem to be:

  1. the inability of callbacks to return values.
  2. the inability to transfer data between windows easily.

I assume that the reason I see these as issues is because I’m using functions a lot more than I’m using classes. For instance, my “load tileset” window is handled entirely functionally: Clicking the menu option in the main window calls the function that loads the new window. From within that window, I create an open file dialog when looking for the image, and modify the canvas displaying the image when I press the enter key (so that it draws the appropriate grid over the image). function function function.

What looks like really bad practice to me is the inclusion of extra arguments to compensate. For example, when I create a tileset, the instance of the TileSet class created should be sent back to the main window where the appropriate information can be displayed. I have a list of loaded tilesets as a global variable (even more bad practice: Everything dealing with my root window is in the global scope! yay!), and because callback functions don’t return values, I pass that list as an argument to my “load tileset window” function, which then passes the argument to the create tileset function (called when you click the appropriate button in the window), where it’s actually needed so that I can add my newly created tileset to the list. Passing arguments through a function ‘hierarchy’ like that seems like a horrible idea. It gets confusing, it’s horrible for writing modular code, and just generally seems unnecessary.

My attempt at fixing the problem would be to write a class representing the whole GUI, and custom made window classes (that the GUI class can create and reference) that can actually store relevant data. That should take care of issues with transferring data between windows. Hopefully it would cut down on my gratuitous use of lambda functions in callbacks as well.
But I’m wondering: is this the best way? Or at least close? I’d rather not start rewriting and then end up with another system that’s just sloppy and confusing in a different way. I know my methods are bad, but I don’t really know what the best approach would be. I’m getting a lot of advice on how to do specific things, but none on how to structure the program as a whole. Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:25:06+00:00Added an answer on May 26, 2026 at 6:25 am

    It sounds like you’re trying to create a GUI that acts procedurally, which won’t work. GUIs aren’t procedural, their code doesn’t run linearly where functions call callbacks which return values. What you’re asking isn’t unique to tkinter. This is the nature of event based GUI programming — callbacks can’t return anything because the caller is an event rather than a function.

    Roughly speaking, you must use a global object of some sort to store your data. Typically this is called the “Model”. It can be a global variable, or it might be a database, or it can be an object of some sort. In any case, it must exist “globally”; that is, it must be accessible to the whole GUI.

    Often, this access is provided by a third component called a “Controller”. It is the interface between the GUI (the “View”) and the data (the “Model”). These three components make up what is called the model-view-controller pattern, or MVC.

    The model, view and controller don’t have to be three different objects. Often, the GUI and the controller are the same object. For small programs this works quite well — the GUI components talk directly to your data model.

    For example, you could have a class that represents a window which inherits from Tkinter.Toplevel. It can have an attribute that represents the data being edited. When the user selects “New” from a main window, it does something like self.tileset = TileSet(filename). That is, it sets the attribute named tileset of the GUI object named self to be an instance of the TileSet class specific to the given filename. Later functions that manipulate the data use self.tileset to access the object. For functions that live outside the main window object (for example, a “save all” function from the main window) you can either pass this object as an argument, or use the window object as the controller, asking it to do something to its tileset.

    Here’s a brief example:

    import Tkinter as tk
    import tkFileDialog
    import datetime
    
    class SampleApp(tk.Tk):
        def __init__(self, *args, **kwargs):
            tk.Tk.__init__(self, *args, **kwargs)
            self.windows = []
            menubar = tk.Menu(self)
            self.configure(menu=menubar)
            fileMenu = tk.Menu(self)
            fileMenu.add_command(label="New...", command=self.new_window)
            fileMenu.add_command(label="Save All", command=self.save_all)
            menubar.add_cascade(label="Window", menu=fileMenu)
            label = tk.Label(self, text="Select 'New' from the window menu")
            label.pack(padx=20, pady=40)
    
        def save_all(self):
            # ask each window object, which is acting both as 
            # the view and controller, to save it's data
            for window in self.windows:
                window.save()
    
        def new_window(self):
            filename = tkFileDialog.askopenfilename()
            if filename is not None:
                self.windows.append(TileWindow(self, filename))
    
    class TileWindow(tk.Toplevel):
        def __init__(self, master, filename):
            tk.Toplevel.__init__(self, master)
            self.title("%s - Tile Editor" % filename)
            self.filename = filename
            # create an instance of a TileSet; all other
            # methods in this class can reference this
            # tile set
            self.tileset = TileSet(filename)
            label = tk.Label(self, text="My filename is %s" % filename)
            label.pack(padx=20, pady=40)
            self.status = tk.Label(self, text="", anchor="w")
            self.status.pack(side="bottom", fill="x")
    
        def save(self):
            # this method acts as a controller for the data,
            # allowing other objects to request that the 
            # data be saved
            now = datetime.datetime.now()
            self.status.configure(text="saved %s" % str(now))
    
    class TileSet(object):
        def __init__(self, filename):
            self.data = "..."
    
    if __name__ == "__main__":
        app = SampleApp()
        app.mainloop()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hopefully this doesn't sound ridiculous, but are some parts of the web.config not required
I seem to be getting an error I can't really understand. Hopefully this doesn't
everyone, i have no idea why this doesn't work hopefully someone here can help
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
Hopefully this will not spark a religious war... We have a web based app
Hopefully this is a simple one, but can anyone provide some simple c# code
Hopefully this still falls within StackOverflow's umbrella! I'm looking to create a quick boot
Hopefully this is a really quick one ;) I have written a lexer /
Hopefully this is simple: I have a relatively long list where each list item
hopefully this will be an easy answer for some of you CSS veterans out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.