Re: [Tutor] Traffic Light
On Sat, 2013-01-19 at 12:31 -0800, anthonym wrote: > Hello All, > > I am new to Python and taking a course now. One of my exercises is to > create a traffic light using tkinter. The exercise also requires that I > create radio buttons that allow the user to click on the color and the > corresponding light will come on. A side-note here rather than a direct contribution to your problem at hand: radio buttons are not a good way of implementing traffic lights if internationalization is a factor. In the UK and other places, there is a state where both red and yellow are on at the same time. -- Russel. = Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.win...@ekiga.net 41 Buckmaster Roadm: +44 7770 465 077 xmpp: rus...@winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder signature.asc Description: This is a digitally signed message part ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traffic Light
That must be what I am missing. How do I call the functions so I can have >the shapes appear on the canvas? > >AG> the same way you call all the other functions. >AG> The bit I can't answer is *where* you call them. I'm not sure >AG> where in your code you want them. >AG> Is it before you press the buttons or is it when you press them? > >On 1/19/13 5:02 PM, "Alan Gauld" wrote: > >>The shapes aren't there because you don't draw them. you have some >>functions that might draw shapes but you never call them. > >> >>> # Display a rectangle >>> def displayRect(self): >>> self.canvas.create_rectangle(10, 10, 100, 100, tages = "rect") >>> >>> # Display a Oval for the top light >>> def displayOval(self): >>> self.canvas.create_oval(10, 10, 10, 10) > >AG> So somewhere you need to write >AG> >AG> self.displayRect() > > >AG> and >AG> >AG>self.displayOval() >AG> >AG>Except you probably want to modify them so you do it with the location: >AG> >AG>self.displayOval(10,10,10,10) > >-- >Alan G >Author of the Learn to Program web site >http://www.alan-g.me.uk/ >___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traffic Light
On 19/01/13 23:29, anthonym wrote: Sure thing. Here is the code. And after that is the box I get with the radio buttons but no shapes. It all runs fine for me. I'm not sure what you are expecting but it does exactly what I'd expect... The shapes aren't there because you don't draw them. you have some functions that might draw shapes but you never call them. And the drawOval function gets overwritten twice, so only the last version actually exists. You need to call that function with the params or rename it something like drawTopLeftOval(). But frankly the generic version is better, just keep it to one and pass the values in. # Display a rectangle def displayRect(self): self.canvas.create_rectangle(10, 10, 100, 100, tages = "rect") # Display a Oval for the top light def displayOval(self): self.canvas.create_oval(10, 10, 10, 10) # Display an Oval for the middle light def displayOval(self): self.canvas.create_oval(20, 20, 20, 20) # Display an Oval for the bottom light def displayOval(self): self.canvas.create_oval(30, 30, 30, 30) Only the last version is actually available in your program it overwrites the earlier versions. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traffic Light
On Sun, Jan 20, 2013 at 12:05 AM, Matthew Ngaha wrote: > On Sat, Jan 19, 2013 at 11:29 PM, anthonym wrote: >> Sure thing. Here is the code. And after that is the box I get with the >> radio buttons but no shapes. > i might be way off as im struggling to understand how tkinter works, but i noticed a few things which hopefully might help. starting with you wanting the buttons on the bottom of the window and not at the top where they are showing. >> from tkinter import * # Import tkinter >> >> class Trafficlight: >> def __init__(self): >> window = Tk() >> self.canvas = Canvas(window, width = 480, height = 480, bg = >> "white") >> self.canvas.pack() >> >> # Add three radio buttons to frame1 >> frame1 = Frame(window) >> frame1.pack() >> >> rbRed = Radiobutton(frame1, text = "Red", bg = "red", >> variable = self.v2, >> value = 1, >> command = self.processRadiobutton) >> rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow", >> variable = self.v2, value = 2, >> command = self.processRadiobutton) >> rbGreen = Radiobutton(frame1, text = "Green", bg = "green", >> variable = self.v2, value = 3, >> command = self.processRadiobutton) >> >> rbRed.grid(row = 400, column = 1) >> rbYellow.grid(row = 400, column = 2) >> rbGreen.grid(row = 400, column = 3) >> > row = 400 seems a bit odd? do you really have 399 occupied rows? you define the canvas with absolute positioning: Canvas(window, width = 480, height = 480). where you trying to place the buttons with absolute positioning also? it seems they are appearing inside the frame in which you have put them in so why are you trying to move them again? im guessing you would have to reposition the Frame itself. also you are not using instance variables(self) for either the frame or the buttons, maybe they lose the reference to them once moved outside the frame. as for your shapes not appearing: > >>def displayRect(self): >>self.canvas.create_rectangle(10, 10, 100, 100, tages = "rect") >> >> # Display a Oval for the top light >>def displayOval(self): >> self.canvas.create_oval(10, 10, 10, 10) >> # Display an Oval for the middle light >>def displayOval(self): >> self.canvas.create_oval(20, 20, 20, 20) >> # Display an Oval for the bottom light >>def displayOval(self): >> self.canvas.create_oval(30, 30, 30, 30) > If that is the complete code you showed.. Your buttons are not doing anything. they are calling a method that does nothing: def processRadiobutton(self): pass also i dont see anywhere in your code where you are calling any of those methods that create the shapes. you have to fix your method that your buttons are calling, and have it call the correct method for your specific shape. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traffic Light
Sure thing. Here is the code. And after that is the box I get with the radio buttons but no shapes. from tkinter import * # Import tkinter class Trafficlight: def __init__(self): window = Tk()# Create a window window.title("Traffic Light") # Set a title # Place canvas in the window self.canvas = Canvas(window, width = 480, height = 480, bg = "white") self.canvas.pack() # Add three radio buttons to frame1 frame1 = Frame(window) # Create and add a frame to window frame1.pack() self.v1 = IntVar() self.v2 = IntVar() rbRed = Radiobutton(frame1, text = "Red", bg = "red", variable = self.v2, value = 1, command = self.processRadiobutton) rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow", variable = self.v2, value = 2, command = self.processRadiobutton) rbGreen = Radiobutton(frame1, text = "Green", bg = "green", variable = self.v2, value = 3, command = self.processRadiobutton) rbRed.grid(row = 400, column = 1) rbYellow.grid(row = 400, column = 2) rbGreen.grid(row = 400, column = 3) window.mainloop()# Create an event loop # Display a rectangle def displayRect(self): self.canvas.create_rectangle(10, 10, 100, 100, tages = "rect") # Display a Oval for the top light def displayOval(self): self.canvas.create_oval(10, 10, 10, 10) # Display an Oval for the middle light def displayOval(self): self.canvas.create_oval(20, 20, 20, 20) # Display an Oval for the bottom light def displayOval(self): self.canvas.create_oval(30, 30, 30, 30) # Add Radio Button process below once I figure that out def processRadiobutton(self): pass Trafficlight() from tkinter import * # Import tkinter class Trafficlight: def __init__(self): window = Tk()# Create a window window.title("Traffic Light") # Set a title # Place canvas in the window self.canvas = Canvas(window, width = 480, height = 480, bg = "white") self.canvas.pack() # Add three radio buttons to frame1 frame1 = Frame(window) # Create and add a frame to window frame1.pack() self.v1 = IntVar() self.v2 = IntVar() rbRed = Radiobutton(frame1, text = "Red", bg = "red", variable = self.v2, value = 1, command = self.processRadiobutton) rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow", variable = self.v2, value = 2, command = self.processRadiobutton) rbGreen = Radiobutton(frame1, text = "Green", bg = "green", variable = self.v2, value = 3, command = self.processRadiobutton) rbRed.grid(row = 400, column = 1) rbYellow.grid(row = 400, column = 2) rbGreen.grid(row = 400, column = 3) window.mainloop()# Create an event loop # Display a rectangle def displayRect(self): self.canvas.create_rectangle(10, 10, 100, 100, tages = "rect") # Display a Oval for the top light def displayOval(self): self.canvas.create_oval(10, 10, 10, 10) # Display an Oval for the middle light def displayOval(self): self.canvas.create_oval(20, 20, 20, 20) # Display an Oval for the bottom light def displayOval(self): self.canvas.create_oval(30, 30, 30, 30) # Add Radio Button process below once I figure that out def processRadiobutton(self): pass Trafficlight() On 1/19/13 3:14 PM, "Matthew Ngaha" wrote: >On Sat, Jan 19, 2013 at 10:56 PM, anthonym wrote: >> Thanks again for the info Alan. I am still passing the button process >> until I can get my rectangle and ovals on the canvass. The program runs >> and produces a window with the radio buttons on top. I would like them >>on >> the bottom but changes to the row field have no effect. >> >> Anybody have any ideas on why I don't see my shape on the canvass? > >i doubt i can help, but it might help if you could paste the section >of the code in relation to what you're explaining and tell us what >it's doing? some people don't use tkinter so its a bit hard to >understand what you're saying. >___ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traffic Light
On Sat, Jan 19, 2013 at 10:56 PM, anthonym wrote: > Thanks again for the info Alan. I am still passing the button process > until I can get my rectangle and ovals on the canvass. The program runs > and produces a window with the radio buttons on top. I would like them on > the bottom but changes to the row field have no effect. > > Anybody have any ideas on why I don't see my shape on the canvass? i doubt i can help, but it might help if you could paste the section of the code in relation to what you're explaining and tell us what it's doing? some people don't use tkinter so its a bit hard to understand what you're saying. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traffic Light
Thanks again for the info Alan. I am still passing the button process until I can get my rectangle and ovals on the canvass. The program runs and produces a window with the radio buttons on top. I would like them on the bottom but changes to the row field have no effect. Anybody have any ideas on why I don't see my shape on the canvass? Thanks, Tony On 1/19/13 1:02 PM, "Alan Gauld" wrote: >On 19/01/13 20:31, anthonym wrote: > >> rbRed = Radiobutton(frame1, text = "Red", bg = "red", >> variable = self.v2, >> value = 1, >> command = self.processRadiobutton) > >Here you assign your method to the button > >> # Add Radio Button process below once I figure that out > >But you haven't defined it, it does not exist. >So python complains. > >You need to define something, even just a pass: > > > def processRadioButton(self): pass > > >> 16, in __init__ >> command = self.processRadiobutton) >> AttributeError: 'Trafficlight' object has no attribute >>'processRadiobutton' > > >HTH > >-- >Alan G >Author of the Learn to Program web site >http://www.alan-g.me.uk/ > >___ >Tutor maillist - Tutor@python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Traffic Light
On 19/01/13 20:31, anthonym wrote: rbRed = Radiobutton(frame1, text = "Red", bg = "red", variable = self.v2, value = 1, command = self.processRadiobutton) Here you assign your method to the button # Add Radio Button process below once I figure that out But you haven't defined it, it does not exist. So python complains. You need to define something, even just a pass: def processRadioButton(self): pass 16, in __init__ command = self.processRadiobutton) AttributeError: 'Trafficlight' object has no attribute 'processRadiobutton' HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Traffic Light
Hello All, I am new to Python and taking a course now. One of my exercises is to create a traffic light using tkinter. The exercise also requires that I create radio buttons that allow the user to click on the color and the corresponding light will come on. I followed some examples in my book to write the code but get some errors that I don't understand . Code and error messages below Thanks, Tony from tkinter import * # Import tkinter class Trafficlight: def __init__(self): window = Tk()# Create a window window.title("Traffic Light") # Set a title # Add three radio buttons to frame1 frame1 = Frame(window) # Create and add a frame to window frame1.pack() self.v1 = IntVar() self.v2 = IntVar() rbRed = Radiobutton(frame1, text = "Red", bg = "red", variable = self.v2, value = 1, command = self.processRadiobutton) rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow", variable = self.v2, value = 2, command = self.processRadiobutton) rbGreen = Radiobutton(frame1, text = "Green", bg = "green", variable = self.v2, value = 3, command = self.processRadiobutton) rbRed.grid(row = 10, column = 1) rbYellow.grid(row = 10, column = 2) rbGreen.grid(row = 10, column = 3) # Add Radio Button process below once I figure that out # Place canvas in the window self.canvas = Canvas(window, width = 480, height = 480, bg = "white") self.canvas.pack() # Display a rectangle def displayRect(self): self.canvas.create_rectangle(10, 10, 190, tages = "rect") # Display a Oval for the top light def displayOval(self): self.canvas.create_oval(10, 10, 10, 10) # Display an Oval for the middle light def displayOval(self): self.canvas.create_oval(20, 20, 20, 20) # Display an Oval for the bottom light def displayOval(self): self.canvas.create_oval(30, 30, 30, 30) # Create an event loop Trafficlight() Error messages Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 02:56:36) [GCC 4.2.1 (Apple Inc. build 5577)] on darwin Type "copyright", "credits" or "license()" for more information. >>> RESTART >>> Traceback (most recent call last): File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 57, in Trafficlight() File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 16, in __init__ command = self.processRadiobutton) AttributeError: 'Trafficlight' object has no attribute 'processRadiobutton' >>> RESTART >>> Traceback (most recent call last): File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 57, in Trafficlight() File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 16, in __init__ command = self.processRadiobutton) AttributeError: 'Trafficlight' object has no attribute 'processRadiobutton' >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor