473,395 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
classes.

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 *
4:
5: data = Database()
6:
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.py", 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?

Cheers,
Richard
Jul 19 '05 #1
1 2891
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.__init__? 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.data = 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

2
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....
2
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...
2
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...
8
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...
3
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...
2
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...
5
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',...
3
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,...
7
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,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.