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.Top level):
def __init__(self,p arent):
Tkinter.Topleve l.__init__(self ,parent)
self.parent = parent
self.initialize (parent)
def initialize(self ,parent):
#create a menu
self.menu = Tkinter.Menu(se lf)
self.config(men u=self.menu)
self.menu.filem enu = Tkinter.Menu(se lf.menu)
self.menu.add_c ascade(label="F ile", menu=self.menu. filemenu)
#for later use
#self.menu.file menu.add_separa tor()
self.menu.filem enu.add_command (label="Exit",
command=self.ki ll)
self.menu.helpm enu = Tkinter.Menu(se lf.menu)
self.menu.add_c ascade(label="H elp", menu=self.menu. helpmenu)
self.menu.helpm enu.add_command (label="About.. .",
command=self.ca llback)
#plotting canvas creation
self.axis = SimplePlot(self ,1000,500)
#status bar
self.status = StatusBar(self)
self.resizable( width=Tkinter.F ALSE,height=Tki nter.FALSE)
def callback(self):
#calls the function within status bar to set the new text,
uses a tuple
self.status.set text("%s %s","This callback","hold s a place for
now!")
def kill(self):
self.parent.qui t()
self.parent.des troy()
def plot_data(self, data):
self.axis.plot( data)
class StatusBar(Tkint er.Frame):
#initializes and draws
def __init__(self,p arent):
Tkinter.Frame._ _init__(self, parent)
self.parent = parent
self.label = Tkinter.Label(s elf.parent, bd=1,
relief=Tkinter. SUNKEN, anchor=Tkinter. W,text='None')
self.label.pack (fill=Tkinter.X )
def settext(self, format,*args):
self.label.conf ig(text=format % args)
self.label.upda te_idletasks()
def clear(self):
self.label.conf ig(text="")
self.label.upda te_idletasks()
class SimplePlot(Tkin ter.Frame):
"Creates a simple plot frame of time<10 and V<5 of pixel size wxh"
def __init__(self,p arent,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.pac k(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.cre ate_line(i,0,i, self.canvas.h)
for i in range(100,self. canvas.h,100):
self.canvas.cre ate_line(0,i,se lf.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.cre ate_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(dat a)
#loop until destroy
a.mainloop()
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.Top level):
def __init__(self,p arent):
Tkinter.Topleve l.__init__(self ,parent)
self.parent = parent
self.initialize (parent)
def initialize(self ,parent):
#create a menu
self.menu = Tkinter.Menu(se lf)
self.config(men u=self.menu)
self.menu.filem enu = Tkinter.Menu(se lf.menu)
self.menu.add_c ascade(label="F ile", menu=self.menu. filemenu)
#for later use
#self.menu.file menu.add_separa tor()
self.menu.filem enu.add_command (label="Exit",
command=self.ki ll)
self.menu.helpm enu = Tkinter.Menu(se lf.menu)
self.menu.add_c ascade(label="H elp", menu=self.menu. helpmenu)
self.menu.helpm enu.add_command (label="About.. .",
command=self.ca llback)
#plotting canvas creation
self.axis = SimplePlot(self ,1000,500)
#status bar
self.status = StatusBar(self)
self.resizable( width=Tkinter.F ALSE,height=Tki nter.FALSE)
def callback(self):
#calls the function within status bar to set the new text,
uses a tuple
self.status.set text("%s %s","This callback","hold s a place for
now!")
def kill(self):
self.parent.qui t()
self.parent.des troy()
def plot_data(self, data):
self.axis.plot( data)
class StatusBar(Tkint er.Frame):
#initializes and draws
def __init__(self,p arent):
Tkinter.Frame._ _init__(self, parent)
self.parent = parent
self.label = Tkinter.Label(s elf.parent, bd=1,
relief=Tkinter. SUNKEN, anchor=Tkinter. W,text='None')
self.label.pack (fill=Tkinter.X )
def settext(self, format,*args):
self.label.conf ig(text=format % args)
self.label.upda te_idletasks()
def clear(self):
self.label.conf ig(text="")
self.label.upda te_idletasks()
class SimplePlot(Tkin ter.Frame):
"Creates a simple plot frame of time<10 and V<5 of pixel size wxh"
def __init__(self,p arent,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.pac k(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.cre ate_line(i,0,i, self.canvas.h)
for i in range(100,self. canvas.h,100):
self.canvas.cre ate_line(0,i,se lf.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.cre ate_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(dat a)
#loop until destroy
a.mainloop()
Comment