473,378 Members | 1,619 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,378 software developers and data experts.

Tkinter- Possibly a basic question

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 code. It will eventually be a voltage measurement using an
Arduino board. Just a simple plot for now.

import Tkinter, time
class App(Tkinter.Toplevel):
def __init__(self,parent):
Tkinter.Toplevel.__init__(self,parent)
self.parent = parent
self.initialize(parent)
def initialize(self,parent):
#create a menu
self.menu = Tkinter.Menu(self)
self.config(menu=self.menu)

self.menu.filemenu = Tkinter.Menu(self.menu)
self.menu.add_cascade(label="File", menu=self.menu.filemenu)
#for later use
#self.menu.filemenu.add_separator()
self.menu.filemenu.add_command(label="Exit",
command=self.kill)

self.menu.helpmenu = Tkinter.Menu(self.menu)
self.menu.add_cascade(label="Help", menu=self.menu.helpmenu)
self.menu.helpmenu.add_command(label="About...",
command=self.callback)

#plotting canvas creation
self.axis = SimplePlot(self,1000,500)

#status bar
self.status = StatusBar(self)

self.resizable(width=Tkinter.FALSE,height=Tkinter. FALSE)

def callback(self):
#calls the function within status bar to set the new text,
uses a tuple
self.status.settext("%s %s","This callback","holds a place for
now!")
def kill(self):
self.parent.quit()
self.parent.destroy()

def plot_data(self,data):
self.axis.plot(data)

class StatusBar(Tkinter.Frame):
#initializes and draws
def __init__(self,parent):
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.label = Tkinter.Label(self.parent, bd=1,
relief=Tkinter.SUNKEN, anchor=Tkinter.W,text='None')
self.label.pack(fill=Tkinter.X)
def settext(self, format,*args):
self.label.config(text=format % args)
self.label.update_idletasks()

def clear(self):
self.label.config(text="")
self.label.update_idletasks()

class SimplePlot(Tkinter.Frame):
"Creates a simple plot frame of time<10 and V<5 of pixel size wxh"
def __init__(self,parent,w,h):
#this line was taken from online... not sure why it works,
#but it allows packing outside of this __init__
Tkinter.Frame.__init__(self, parent)
self.parent = parent
self.canvas = Tkinter.Canvas(parent,width=w,height=h)

#frame height in pixels
self.canvas.h = h

#frame width in pixels
self.canvas.w = w
self.canvas.pack(fill=Tkinter.X)

#draw gridlines
self.gridon()

def gridon(self):
"Draws gridlines on the plot at every 1 unit"
for i in range(100,self.canvas.w,100):
self.canvas.create_line(i,0,i,self.canvas.h)
for i in range(100,self.canvas.h,100):
self.canvas.create_line(0,i,self.canvas.w,i)

def plot(self, data):
"Plots data given as data = [], data.append( (x,y) )"
for x, y in data:
px = int(x/10*self.canvas.w)
py = int(self.canvas.h-y/5*self.canvas.h)
self.canvas.create_rectangle((px-1, py-1, px+1, py+1),
outline="red")
if __name__ == "__main__":
root = Tkinter.Tk()
root.withdraw()

#create main window
a = App(root)
a.title('Plot')

#create a sample data range for testing
#data ranges from x=0, y=0 to x=10, y=5
data = []
for i in range(1000):
data.append( (float(i)/1000*10,float(i)/1000*5) )

a.plot_data(data)

#loop until destroy
a.mainloop()

Jul 30 '08 #1
3 2946
<jo*****@gmail.comwrote in message
news:59**********************************@26g2000h sk.googlegroups.com...
>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.
At the risk of overlooking something obvious, why don't you make your
App class a subclass of Tkinter.Frame instead of Tkinter.Toplevel, and
just pack it into the root window instead of worrying about how to get rid
of the root window? Just delete "root.withdraw()" and then add "a.pack()"
before "a.mainloop()".

Russ

Jul 30 '08 #2
On Wed, Jul 30, 2008 at 6:33 PM, <jo*****@gmail.comwrote:
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?
You don't.
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 code. It will eventually be a voltage measurement using an
Arduino board. Just a simple plot for now.

import Tkinter, time

...

if __name__ == "__main__":
root = Tkinter.Tk()
root.withdraw()

#create main window
a = App(root)
a.title('Plot')

#create a sample data range for testing
#data ranges from x=0, y=0 to x=10, y=5
data = []
for i in range(1000):
data.append( (float(i)/1000*10,float(i)/1000*5) )

a.plot_data(data)

#loop until destroy
a.mainloop()
Before anything.. root withdraw works here but I wouldn't do it
anyway. There are several solutions to this actually, I will list
some, and none involve withdrawing the root window.

The first thing you could do is change App to not inherit from
Toplevel, instead change it to inherit object at max and pass the root
object to it (to act as the parent for menu and other things). The
drawback here is that you will have to change several lines, those
that involve menu creation for example and this a.title and
a.mainloop.

The second option is to not create the root there, instead, make App
inherit Tk. I rarely see people doing this, but it works too. Here you
won't need to store the parent in an instance attribute, given it is
always accessible through self.master since you are subclassing Tk.

Another option is to make App subclass Frame instead of Toplevel, so
you don't create an unecessary window. But if it is really a window,
it doesn't make much sense to inherit Frame, so we are back at the
first proposed solution.

--
-- Guilherme H. Polo Goncalves
Jul 30 '08 #3
On Jul 30, 6:48*pm, "Guilherme Polo" <ggp...@gmail.comwrote:
On Wed, Jul 30, 2008 at 6:33 PM, *<josh...@gmail.comwrote:
...

...

The second option is to not create the root there, instead, make App
inherit Tk. I rarely see people doing this, but it works too. Here you
won't need to store the parent in an instance attribute, given it is
always accessible through self.master since you are subclassing Tk.

...

--
-- Guilherme H. Polo Goncalves
Excellent, this one was easy enough for me to understand/implement and
it worked. Thanks for the help.
Jul 31 '08 #4

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

Similar topics

1
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...
3
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...
2
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....
7
by: SeeBelow | last post by:
Do many people think that wxPython should replace Tkinter? Is this likely to happen? I ask because I have just started learning Tkinter, and I wonder if I should abandon it in favor of...
2
by: codecraig | last post by:
Hi, I was reading through the Tkinter tutorial at http://www.pythonware.com/library/tkinter/introduction/index.htm ...and it mentions that by doing, from Tkinter import * you have access to...
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
1
by: yvesd | last post by:
hello i want to intercept tkinter python system events like wm_delete_window and if possible for any window, but the newest code I've produced give me an error : Traceback (most recent call...
3
by: seanacais | last post by:
I'm trying to build an unknown number of repeating gui elements dynamically so I need to store the variables in a list of dictionaries. I understand that Scale "variable" name needs to be a...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.