[Tutor] Traffic Light

Matthew Ngaha chigga101 at gmail.com
Sun Jan 20 01:06:49 CET 2013


On Sun, Jan 20, 2013 at 12:05 AM, Matthew Ngaha <chigga101 at gmail.com> wrote:
> On Sat, Jan 19, 2013 at 11:29 PM, anthonym <anthonym at att.net> 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.


More information about the Tutor mailing list