Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

help putting shapes on a gui

Thu Apr 23, 2015 9:11 pm

hi all, im trying to put 4 squares and 4 circles on a gui to later represent as gpio inputs with different colours.
id like to align 2 squares and 2 circles on each row currently occupied by the start button and label but cannot get anything to successfully work alongside what I already have

Code: Select all

from Tkinter import *


class Grid(Frame):
    def var1(self):
        self.label4String.set("Panel Test Began")
        if self.button1["text"] == "Begin Panel Test":
            self.label4.config(bg='green', fg='black')
            self.after(2)
            print "You have started the panel test:", self.label4String.get()
            self.button1["text"] = "Stop Panel Test"
        else:
            self.button1["text"] = "Begin Panel Test"
            self.label4.config(bg='dark red', fg='white')
            self.after(2)
            self.label4String.set("Panel Test Ended")
            print "You have stopped the panel test:", self.label4String.get()

    def __init__(self):
        Frame.__init__(self)
        self.master.title( "Grid")
        self.variable = "Start Variable"

        self.master.rowconfigure(0, weight=1)
        self.master.columnconfigure(0, weight=1)
        self.grid(sticky=W+E+N+S)

        self.button1 = Button(self, text = "Begin Panel Test", command=self.var1)
        self.button1.grid(row=1, column=1,  sticky=W+E+N+S)

        self.button2 = Button(self, text="Quit", command=exit)
        self.button2.grid(row=1, column=2, sticky=W+E+N+S)

        self.label4String = StringVar()
        self.label4 = Label(self, textvariable=self.label4String)
        self.label4.grid(row=2, column=1, columnspan=2, sticky=W+E+N+S)

        self.rowconfigure(1, weight=1)
        self.columnconfigure(1, weight=1)


def main():

    Grid().mainloop()

if __name__ == '__main__':
    main()

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: help putting shapes on a gui

Fri Apr 24, 2015 11:18 am

Have a look at my python code that creates a Tkinter UI for controlling the GPIO pins. I created an LED class which draws a circle which is filled red or green depending on the state of the GPIO.

viewtopic.php?f=32&t=41639

If you get stuck, let me know.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: help putting shapes on a gui

Sat Apr 25, 2015 12:58 pm

Thanks scotty, that's some intense code. I can read and understand most of it but re-writing the parts I want into a structure which works with the code I have is proving tough.
I've been trying to put extractions and manipulations of your code into mine such as the following:
(but im unsure if my manipulations are legal or just nonsense code, im very new to this)

Code: Select all

        self.configure()
        self.c = Canvas(self,width=self['width'],height=self['height'])
        self.c.grid()
        self.led = self._drawcircle(10, 20, 20)
        self.led.grid(column=3, row=0)

    def _drawcircle(self,x,y,rad):
        color = "red"
        return self.c.create_oval(x-rad, y-rad, x+rad, y+rad, width=rad/5, fill=color, outline='black')

    def _change_color(self):
        if self.button1 == TRUE:
            color = "green"
        else:
            color = "red"
        self.c.itemconfig(self.led, fill=color)

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: help putting shapes on a gui

Mon Apr 27, 2015 12:40 pm

Things with Tkinter get essier once you embrace classes. The LED widget is a class in the same way that a Tkinter Button is a class.
Here is a slightly more simple example that changes the colour of an LED at random each time a button is pressed

Code: Select all

import sys
import random
if(sys.version_info[0]<3):
    from Tkinter import *
else:
    from tkinter import *

class Circle(Frame):
    """A Tkinter Circle Widget.
    a = Circle(root,size=10)
    """
    
    def __init__(self,master,size=10,color="red",**kw):
        self.size = size
        self.color = color
        Frame.__init__(self,master,width=size+5,height=size+5)
        self.configure(**kw)
        self.c = Canvas(self,width=self['width'],height=self['height'])
        self.c.grid()
        self.led = self._drawcircle((self.size/2)+1,(self.size/2)+1,(self.size-1)/2)
    def _drawcircle(self,x,y,rad):
        """Draws the circle initially"""
        color=self.color
        return self.c.create_oval(x-rad,y-rad,x+rad,y+rad,width=rad/5,fill=color,outline='black')
    def change_color(self,color=None):
        """Updates the LED colour"""
        self.c.itemconfig(self.led, fill=color)

def new_random_color():
    global circle
    colors = ["red","blue","yellow","green","orange"]
    picked = random.choice(colors)
    circle.change_color(color=picked)

if __name__ == '__main__':
    root = Tk()
    root.title("Circle Color Color Widget")
    circle = Circle(root,size=30,color="black")
    circle.grid(row=0,column=0)
    button1 = Button(root,text="Change Color",command=new_random_color)
    button1.grid(row=0,column=1)
    root.mainloop()

Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”