Tkinter 5 x5 gui

Post here if you need help with creating a Graphical User Interface in Python.

Tkinter 5 x5 gui

Postby cossie2k » Sat Aug 24, 2013 5:16 pm

Hi Everyone,

I hope you are all OK.

Im afraid Im new to python and programming in general, so you may well be seeing quite a bit of me on here!

Heres my problem....

My original 3 x 3 (working) code was this...
Code: Select all
buttons = []

directions = [NW, N, NE, W, '', E, SW, S, SE]

canvas = Canvas(highlightthickness=0, height=height, width=width)
canvas.master.title("3 x 3 Matrix")

canvas.grid()


I want to increase this to 5 x 5 for the time being. Following a good old googling session, I have tried this....

Code: Select all
buttons = []

directions = [['O', 'O', 'O', 'O', 'O'], ['O', 'O', 'O', 'O', 'O'], ['O', '0', 'O', 'O', 'O'], ['O', '0', 'O', 'O', 'O'], ['O', '0', 'O', 'O', 'O']]

canvas = Canvas(highlightthickness=0, height=height, width=width)
canvas.master.title("5 x 5 Matrix")

canvas.grid()    def __init __(self, path):
        self.connection = serial.Serial(path, 9600)     
        pass


And I have tried this....

Code: Select all
buttons = []

directions = [[0 for x in range(5)] for y in range(5)]

canvas = Canvas(highlightthickness=0, height=height, width=width)
canvas.master.title("5 x 5 Matrix")

canvas.grid()    def __init __(self, path):
        self.connection = serial.Serial(path, 9600)     
        pass


I not sure what I am doing wrong but with my modified versions, I get a syntax error on the def _ init section.

Please could someone advise?

Thanks for any help you can offer
Alex
cossie2k
 
Posts: 3
Joined: Sat Aug 24, 2013 5:06 pm

Re: Tkinter 5 x5 gui

Postby stranac » Sat Aug 24, 2013 5:53 pm

This makes no sense:
Code: Select all
canvas.grid()    def __init __(self, path):

canvas.grid() should be on one line, and the rest on another.

Also, the __init__ part seems out of place here, it belong in a class.

If you post your entire code, maybe you'll get some more specific help.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Tkinter 5 x5 gui

Postby cossie2k » Sat Aug 24, 2013 6:02 pm

Hi,

Thanks for the fast reply.

Heres the original code:-

Code: Select all
import  time, serial, random
from Tkinter import *

height = 600
width = 600

enabled = [1, 1, 1, 1, 1, 1, 1, 1, 1]   #This list cointains the state of each button: 1 = disabled, -1 = enabled

buttons = []

directions = [NW, N, NE, W, '', E, SW, S, SE]

canvas = Canvas(highlightthickness=0, height=height, width=width)
canvas.master.title("3 x 3 Matrix")

canvas.grid()

class Arduino:
    connection = None
    def __init__(self, path):
        self.connection = serial.Serial(path, 9600)     
        pass
    def __del__(self):
        self.connection.close()
        pass
    def isOpen(self):
        return self.connection.isOpen()
    def writeLine(self, line):
        if self.isOpen():
            self.connection.write(line)
            time.sleep(0.1)


arduino = Arduino(3)       #This sets the arduino to serial port 2.  On PC's this will probably be COM3
                           # On macs this port will be something like  /dev/tty.usbmodem
arduino.writeLine('!')     #The arduino program resets all the led's when it recieves '!' over serial

def ledWrite(i):
   print(i)
   arduino.writeLine(str(i))                   
   if enabled[i] == 1:
      buttons[i].configure(bg = '#FF6600')      #Tkinter can take hexidecimal color codes, so you can customize your UI
   else:
      buttons[i].configure(bg = 'white')
   buttons[i].update()
   enabled[i] = enabled[i] * -1

for i in range(9):         #this creates all the buttons, so you don't need a line to create and grid each button
   x = i
   i = Button(height = 10, width = 20,  bg = 'white', relief = FLAT, command = lambda arg=x: ledWrite(arg))    #'arg = x' may seem repetitive, but if it is not included,
                                                                                                               #the buttons pass the final value of x to 'ledWrite' , which is 8
   i.grid(column = 0, row = 0, columnspan = 3, rowspan = 3, sticky = directions[x])
   i.number = x
   buttons.append(i)

print '1'
canvas.mainloop()

arduino.writeLine('!')      #when you exit the UI, all the LED's are reset to 'off'
print '!'
arduino.connection.close()    #The serial connection is also closed when you quit, so you can switch between programming in python and the arduino IDE without resetting your arduino





And this is my modified code

Code: Select all
import  time, serial, random
from Tkinter import *

height = 600
width = 600

enabled = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]   #This list cointains the state of each button: 1 = disabled, -1 = enabled

buttons = []

directions = [[0 for x in range(5)] for y in range(5)]

canvas = Canvas(highlightthickness=0, height=height, width=width)
canvas.master.title("5 x 5 Matrix")

canvas.grid()    def __init __(self, path):
        self.connection = serial.Serial(path, 9600)     
        pass
    def __del__(self):
        self.connection.close()
        pass
    def isOpen(self):
        return self.connection.isOpen()
    def writeLine(self, line):
        if self.isOpen():
            self.connection.write(line)
            time.sleep(0.1)


arduino = Arduino(3)       #This sets the arduino to serial port 2.  On PC's this will probably be COM3
                           # On macs this port will be something like  /dev/tty.usbmodem
arduino.writeLine('!')     #The arduino program resets all the led's when it recieves '!' over serial

def ledWrite(i):
   print(i)
   arduino.writeLine(str(i))                   
   if enabled[i] == 1:
      buttons[i].configure(bg = '#FF6600')      #Tkinter can take hexidecimal color codes, so you can customize your UI
   else:
      buttons[i].configure(bg = 'white')
   buttons[i].update()
   enabled[i] = enabled[i] * -1

for i in range(9):         #this creates all the buttons, so you don't need a line to create and grid each button
   x = i
   i = Button(height = 10, width = 20,  bg = 'white', relief = FLAT, command = lambda arg=x: ledWrite(arg))    #'arg = x' may seem repetitive, but if it is not included,
                                                                                                               #the buttons pass the final value of x to 'ledWrite' , which is 8
   i.grid(column = 0, row = 0, columnspan = 3, rowspan = 3, sticky = directions[x])
   i.number = x
   buttons.append(i)

print '1'
canvas.mainloop()

arduino.writeLine('!')      #when you exit the UI, all the LED's are reset to 'off'
print '!'
arduino.connection.close()    #The serial connection is also closed when you quit, so you can switch between programming in python and the arduino IDE without resetting your arduino





Ill have a quick try with your suggestions though

Thanks again
Alex
cossie2k
 
Posts: 3
Joined: Sat Aug 24, 2013 5:06 pm

Re: Tkinter 5 x5 gui

Postby stranac » Sat Aug 24, 2013 6:11 pm

From looking at your codes, it seems like you accidentally deleted the first 2 lines of the Arduino class.

Also, what's up with those pass statements?
pass does absolutely nothing.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Tkinter 5 x5 gui

Postby cossie2k » Sun Aug 25, 2013 11:03 pm

Ill be honest....

When I taught myself what I needed in VBA, I pinched code off the internet and bent and twisted it to my own devises, learning along the way.

This is just the same so I pinched some code of the instructables site and have been having a play. The "pass" bit is left over from that.

I will have a tweek of the code and see what I can do with your suggestions.

Is there a list any where that would tell me what each statement does? Or would you advise going to basic and getting a beginners guide to programming in c+?

Thanks for all your help
Alex
cossie2k
 
Posts: 3
Joined: Sat Aug 24, 2013 5:06 pm


Return to GUI

Who is online

Users browsing this forum: No registered users and 3 guests