473,762 Members | 6,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class for custom Tkinter widget--difficulty

I'm trying to create a custom Tkinter widget class, and I'm having some
difficulty getting it set up properly.

The class is called MacToolbar, saved in its own module MacToolbar.py,
and imported with this statement:

import MacToolbar

Here is the relevant portion of the class:

###relevant class code

class MacToolbar:
def __init__(self, master):
self.tk.call("p ackage", "require", "macsearchfield ")
self.tk.call('p ackage', 'require', 'tile')
def create(self):
self.baseframe = Tile.Frame(self , master)
self.baseframe. pack(side='top' , fill='both', expand='no')
def toolbar(self):
self.create()
self.newframe = Tile.Frame(self .baseframe)
self.newframe.p ack(fill='both' , expand='yes')

self.buttonfram e = Tile.Frame(self .newframe)
self.buttonfram e.pack(side='to p', fill='both', expand = 'yes')
I'm a bit confused on how to call a class that is imported via a module.
Both approaches I try produce errors:

The first approach:

self.topframe = MacToolbar.tool bar(self.mainfr ame)

yields this error:

AttributeError: 'module' object has no attribute 'toolbar'

The second approach:

self.topframe = MacToolbar.MacT oolbar.toolbar( self.mainframe)

yields this error:

TypeError: unbound method toolbar() must be called with MacToolbar
instance as first argument (got Frame instance instead)

I was expecting to be able to use the standard Class.Method notation in
calling MacToolbar class methods, but it appears that this does not work
at all: that's what I see in the first error above. Instead I have to
use Module.Class.Me thod notation? I've never seen this before.

Can someone point out what I am doing wrong, either in the construction
of the class, the way it's imported, or in how I'm calling methods?

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Sep 17 '07 #1
1 1872
En Mon, 17 Sep 2007 17:11:03 -0300, Kevin Walzer <kw@codebykevin .com>
escribi�:
I'm trying to create a custom Tkinter widget class, and I'm having some
difficulty getting it set up properly.

The class is called MacToolbar, saved in its own module MacToolbar.py,
and imported with this statement:

import MacToolbar

Here is the relevant portion of the class:

###relevant class code

class MacToolbar:
def __init__(self, master):
self.tk.call("p ackage", "require", "macsearchfield ")
self.tk.call('p ackage', 'require', 'tile')
def create(self):
self.baseframe = Tile.Frame(self , master)
self.baseframe. pack(side='top' , fill='both', expand='no')
def toolbar(self):
self.create()
self.newframe = Tile.Frame(self .baseframe)
self.newframe.p ack(fill='both' , expand='yes')

self.buttonfram e = Tile.Frame(self .newframe)
self.buttonfram e.pack(side='to p', fill='both', expand = 'yes')
I'm a bit confused on how to call a class that is imported via a module.
Both approaches I try produce errors:

self.topframe = MacToolbar.tool bar(self.mainfr ame)
self.topframe = MacToolbar.MacT oolbar.toolbar( self.mainframe)
[...] I was expecting to be able to use the standard Class.Method
notation in
calling MacToolbar class methods, but it appears that this does not work
at all: that's what I see in the first error above. Instead I have to
use Module.Class.Me thod notation? I've never seen this before.

Can someone point out what I am doing wrong, either in the construction
of the class, the way it's imported, or in how I'm calling methods?
You have to create an instance, and then you call methods on that instance.
But toolbar() doesn't look like a method; both the create and toolbar
methods appear to create the toolbar itself; probably you should move all
that code into __init__.
Perhaps you should inherit from Frame?

Given the kind of mistakes you have, I suggest you read something about
classes and object orientation in Python. (Maybe you have used a lot of
classes, but perhaps this is the first class you *design*?)

--
Gabriel Genellina

Sep 17 '07 #2

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

Similar topics

1
5983
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles filling the remainder of the window. Mind you, this is a contrived test application to help me understand Tkinter and Python, not an actual application yet. I've trivially subclassed Tkinter.Canvas into ColorCanvas, added a bunch of ColorCanvases...
3
7035
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter import * class App:
2
3250
by: Paul A. Wilson | last post by:
I'm new to Tkinter programming and am having trouble creating a reusable button bar... I want to be able to feed my class a dictionary of button names and function names, which the class will make. My button bar is implemented a Frame subclass, which takes the button dictionary as an argument and displays the buttons on the screen: class OptionsBar(Frame): def __init__(self, buttonDict, parent=None) Frame.__init__(self, parent)
11
7872
by: William Gill | last post by:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep references to them in a 2 dimensional list ( rBtns ). It works fine, and I can even make it so only one button per column can be selected, by assigning each column to an intVar. In many languages a radiobutton has a property that can be directly read to see if it is selected on unselected. Tkinter radiobuttons don't seem to have any such property. Is there any way to...
1
4324
by: Ron Provost | last post by:
Hello, I've written a simple GUI which contains a listbox to hold some information. I've found that the click-selection schemes provided by Tkinter are insufficient for my needs. Essentiall I need to impletement a custom selectMode. As a first go, I've attempted to implement a click-on-click-off mode. So for each entry, a click on that entry toggels its selection mode. A click on any entry does not affect the selection mode of any...
0
1062
by: Alexandre Guimond | last post by:
Hi all. I'm creating a new widget class using Tkinter (class inherited form Tkinter.Frame). This class creates a bunch of other widgets inside it that can be "gridded" either horizontally or vertically. I would like to provide the user of the class an option to change this layout on the fly, using something like "widget = 'vertical'". Does anyone have experience with that? I'm afraid i just down know how to proceed or where to look for...
3
3611
by: dwelch91 | last post by:
I'm trying unsuccessfully to do something in Tk that I though would be easy. After Googling this all day, I think I need some help. I am admittedly very novice with Tk (I started with it yesterday), so I am sure I am overlooking something simple. The basic idea is that my application will consist of a series of modal dialogs, that are chained together in "wizard" fashion. There will be some processing in between dialogs, but for the most...
1
2969
by: Hunter.lennon | last post by:
I want to create a custom scrollbar using particular images, which will then be placed on a canvas to control another window on the canvas. Right now I am inheriting from scrollbar, but I do the movement with custom functions. When I create it and put in into the canvas with "canvas.create_window" a standard scrollbar shows in the correct spot and my custom one is outside of the canvas. All I have right now is something that moves...
3
2983
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
0
9554
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9989
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9811
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 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7358
isladogs
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.