[newbie] advice and comment wanted on first tkinter program

Terry Reedy tjreedy at udel.edu
Fri Jan 17 16:40:42 EST 2014


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





More information about the Python-list mailing list