[newbie] advice and comment wanted on first tkinter program

Jean Dupont jeandupont314 at gmail.com
Sat Jan 18 09:52:24 EST 2014


Op vrijdag 17 januari 2014 22:40:42 UTC+1 schreef Terry Reedy:
> On 1/17/2014 8:20 AM, Jean Dupont wrote:
> 
> > Dear all,
> 
> > I made a simple gui with tkinter. I can imagine there are things which I
> 
> > did which are "not optimal". So what I ask is to comment on my code
> 
> > preferable with snippets of code which show how to do improve my code.
> 
> > #!/usr/bin/env python
> 
> > import Tkinter
> 
> 
> 
> 1. import Tkinter as tk
> 
> 
> 
> Besides saving a bit of writing and reading time later, this makes any 
> 
> future conversion to 3.x easier.
> 
> 
> 
> import tkinter as tk
> 
> 
> 
> 2. add a few spaces to demarcate blocks of code.
> 
> 
> 
> > import time
> 
> > import RPi.GPIO as GPIO
> 
> 
> 
> 2. add a few spaces to demarcate blocks of code, such as here
> 
> 
> 
> > GPIO.setmode(GPIO.BOARD)
> 
> > GPIO.setup(26,GPIO.OUT)
> 
> > GPIO.setup(24,GPIO.OUT)
> 
> > #hardware : connect 2 leds:
> 
> > #board-pin 26 on/off led; control with buttons
> 
> > #board-pin 24 led with pwm dimming and frequency; control via sliders
> 
> 
> 
> and here
> 
> 
> 
> > top = Tkinter.Tk()
> 
> > top.geometry("600x400+310+290")
> 
> 
> 
> This looks strange somehow, but if it works...
> 
> 
> 
> 
> 
> > label1 = Label(top,relief=RAISED,bg =
> 
> > "#EFF980",font=("Helvetica",14),height = 1, width = 15)
> 
> 
> 
> In calls, put spaces after , but not before and after =.
> 
> For other suggestions, see
> 
> http://www.python.org/dev/peps/pep-0008/
> 
> 
> 
> I suspect that the above is one line in your code and the bad wrapping a 
> 
> result of mis-spacing. The following is also one line, but easer to read 
> 
> as spaces separate argument chunks
> 
> 
> 
> label1 = Label(top, relief=RAISED, bg="#EFF980", font=("Helvetica",14), 
> 
> height=1, width=15)
> 
> 
> 
> and the wrapping, if any, does not break up an arg chunk.
> 
> 
> 
> Some people advocate defining an App class, but Tk and tkinter, even 
> 
> though object method based, allow the straightforward imperative style 
> 
> you have used.
> 
> 
> 
> I agree with Peter: "First and foremost a program has to do what the 
> 
> author wants it to do. Everything else is secondary." But a bit of 
> 
> styling will make reading and changing easier.
> 
> 
> 
> -- 
> 
> Terry Jan Reedy

Thanks Peter and Terry Jan for the useful suggestions. One thing which I find a bit weird: when asking for Python-help concerning raspberry pi code or problems, a lot of people don't seem to be interested in helping out, that's of course their choice, but maybe they don't seem to be aware the raspberry pi is often the motivation for starting to learn to program in Python. And as such such a reaction is a bit disappointing.

kind regards,
jean



More information about the Python-list mailing list