473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter app structure

Hi there,

I've just started my first project with Tkinter.

I've already coded all the data handling classes and have a nice
interface to work with (it happens to be a wrapper around the DOM of a
large(ish) XML document but thats probably not important ;-)

I'm new to Tkinter so I've started in a fairly random place: the main
menu. I noticed that the event mechanism was based on callbacks so, in a
flash of inspiration, I decided to create a new module called
'commands.py' which would contain all my callback methods (meaning I
could use them for a toolbar as well, if I ever add one) and which acts
as an intermediary between the Tkinter classes and my data hanlding

I've also created a 'main.py' module which holds the Tk object (called
'root'), the top level data object (called 'data') and the main UI
object (called 'mwnd', an instance of a class inherited from Frame
called 'MainWindow').

* The main.py module imports:

from dataclasses import *
from mainwindow import *
from Tkinter import *

* The mainwindow.py module imports:

from Tkinter import *
from commands import *

* The commands.py modules imports:

import main
import tkMessageBox

* And the dataclasses.py module imports only unrelated modules

This set up should allow me to be able to access main.data and main.root
from the commands.py module and the command callback functions (defined
in commands.py) from mainwindow.py.

main.py looks like this:
=============== =============== ======
1: from dataclasses import *
2: from mainwindow import *
3: from Tkinter import *
5: data = Database()
7: root = Tk()
8: mwnd = MainWindow(root )
9: root.mainloop()
=============== =============== ======

However, when I execute it I get the following error:

Traceback (most recent call last):
File "./main.py", line 2, in ?
from mainwindow import *
File "mainwindow.py" , line 2, in ?
from commands import *
File "commands.p y", line 4, in ?
import main
File "main.py", line 8, in ?
mwnd = MainWindow(root )
NameError: name 'MainWindow' is not defined

(where 'mainwindow.py' line 2 is "from commands import *", commands.py
line 4 is "import main" and the name "MainWindow " is the name of the
class defined in the mainwindow.py module)

Whats going wrong? Is it a 'circular import'? Is there a better way that
I could organise these modules?

Jul 19 '05 #1
1 2904
Richard Lewis wrote:
Hi there,

I've just started my first project with Tkinter. This set up should allow me to be able to access main.data and main.root
from the commands.py module and the command callback functions (defined
in commands.py) from mainwindow.py. <...> Whats going wrong? Is it a 'circular import'? Is there a better way that
I could organise these modules?

Yes, this looks like a circular import to me.

Remember, though, that an import statement in Python isn't completely
analagous to a #include statement in C; because of dynamic typing and
the general guideline of duckitude ("if it walks like a duck and quacks
like a duck..."), you usually don't /need/ to have circular imports.

With the presumption that data = Database() is a mutable object, in
that you interact with it normally and don't reassign it anywhere in
your program, why not just pass data as a parameter to
MainWindow.__in it__? Likewise, MainWindow can call commands with
root/data as parameters -- you could even organize your commands into
an instantiated class, and have the CommandClass take both of these as
__init__ arguments. In the program (MUD Client) that I'm working on, I
have a vaguely similar dependency between interface-agnostic backend
code and the in-development TKinter interface; by wrapping everything
except main() and a few constants inside instantiated classes, I think
I've mostly avoided circular imports.

If you really, truly need data to be a global variable, you could
possibly put it inside its own module, and instantiate it via
data_module.dat a = Database() from main. But that's really ugly, and
I'm not 100% sure that it'd work -- I've never had need to try it, and
am writing this off-the-cuff.

But take this advice all with a lagish grain of iodized NaCl, since I'm
a rank newbie at Python.

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Marcus Schneider | last post by:
Sorry if this is an extremely stupid question: I produced some Python modules with TKinter stuff and now I want to use that from my main program. The windows have wuite some user interaction. It's easy to bring them to the screen, but how do I make the communication (i.e. variables) between the main and the modules happen? Currently I...
by: Anthony Baxter | last post by:
I'm trying to use py2exe and Inno Setup to build an installer for shtoom, which uses tkinter. If I take the py2exe generated directory, and run the executable from there, it works fine. If I add all the files in the directory to an installer (including the tk-8.4 and tcl-8.4 directories), it builds an installer fine. But when I run the...
by: John Pote | last post by:
Running my programme in Python 2.3.4 I received the following msg in the consol :- (Pent III running W2K prof) """ Exception in Tkinter callback Traceback (most recent call last): File "c:\apps\python\234\lib\lib-tk\Tkinter.py", line 1345, in __call__ return self.func(*args) File "c:\apps\python\234\lib\lib-tk\Tkinter.py", line 459, in...
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I happen to already own John Grayson's book about Tkinter programming, so that is rather handy if I decide to use Tkinter. I have done some things...
by: William Gill | last post by:
Working with tkinter, I have a createWidgets() method in a class. Within createWidgets() I create several StringVars() and assign them to the textvariable option of several widgets. Effectively my code structure is: def createWidgets(self): ... var = StringVar() Entry(master,textvariable=var) ...
by: Steve Potter | last post by:
I am trying to find some method of attaching a Listbox object to a list object so as the contents of the list are changed the contents of the Listbox will be updated to match. I have found a few references to something like this in this old post...
by: Kevin Walzer | last post by:
I'm having difficulty structuring a Tkinter menu entry. Here is the command in question: self.finkmenu.add_command(label='Update List of Packages', command=self.authorizeCommand(self.scanPackages)) When I start my program, it crashes because it's trying to run the command self.authorizeCommand. The reason I'm structuring it in this...
by: rahulnag22 | last post by:
Hi, I have developed a GUI using tkinter (grid geometory manager). The structure is a top frame containing multiple subframes. Each subframe has a combination of widgets like(Entry, label, button,listboxes). The subframes are placed with a padx and pady offset with regards to the other subframes. And the widgets within these subframes have...
by: Protected | last post by:
Hello. I'm a complete newbie trying to learn Python. I decided to try some Tkinter examples, including the one from the library reference, but they don't seem to do anything! Shouldn't there be, like, a dialog? I'm running Windows XP and using IDLE. You can assume my version of Python is the latest.
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.