Anyway, we've been focusing on console code but I figured I would pretty things up and teach myself GUI programming. What follows is my first attempt, and is based on a program I've written to calculate inductances and frequency ranges for crystal set coils (a personal hobby). Its pretty basic at this stage, but the code is starting to bloat fairly quickly so I was hoping I could get some feedback on what I have done and what I should be doing (and how I should be doing it) before it gets too big to change stuff around.
Also, I have been having trouble getting the pic to stay in its own cell on the grid. If I move it to any row other than 0 it goes to the bottom of the window. In 0, it goes to the top and covers everything else. Any pointers on showing and image properly would be great
Any thoughts would be gratefully received and much appreciated
Cheers
Steve
#!/usr/local/bin/python
#encoding: utf-8
from Tkinter import *
from decimal import *
from sys import *
from PIL import Image, ImageTk
from math import *
class Application(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
# The initial values should give a good basic coil for the AM band
self.fid = IntVar(self,'40') # Initial inside dia of former = 40mm
self.nt = IntVar(self,'75') # Initial number of turns = 75
self.wd = DoubleVar(self,'0.12') # Initial wire dia = 0.12mm
self.ind = IntVar() # inductance
self.fod = IntVar() # former outside dia
self.wl = IntVar() # wire length
self.minf = IntVar() # min freq using 20-220pF tuning cap
self.maxf = IntVar() # max freq using 20-220pF tuning cap
self.grid()
# Gave up trying to get the pic to go where I wanted it...
#self.image = Image.open("image004.jpg")
#self.pic = ImageTk.PhotoImage(self.image)
self.createMenu()
self.createWidgets()
def createMenu(self):
top = self.winfo_toplevel()
self.menuBar = Menu(top)
top["menu"] = self.menuBar
self.fileMenu = Menu(self.menuBar, tearoff = 0)
self.fileMenu.add_command(label="Quit", command = top.destroy)
self.menuBar.add_cascade(label="File", menu = self.fileMenu)
self.helpMenu = Menu(self.menuBar, tearoff = 0)
self.helpMenu.add_command(label="About", command = self.about)
self.menuBar.add_cascade(label="Help", menu = self.helpMenu)
def createWidgets(self):
top = self.winfo_toplevel()
# Configure the main window to be 'stretchy'
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
# Input box labels 1st column
self.label1 = Label(self, text = "Former inside diameter: ")
self.label1.grid(sticky = 'e', column = 0, row = 0)
self.label2 = Label(self, text = "mm")
self.label2.grid(sticky = 'w', column = 2, row = 0)
self.label3 = Label(self, text = "Wire diameter: ")
self.label3.grid(sticky = 'e', column = 0, row = 1)
self.label4 = Label(self, text = "mm")
self.label4.grid(sticky = 'w', column = 2, row = 1)
self.label5 = Label(self, text = "Number of turns: ")
self.label5.grid(sticky = 'e', column = 0, row = 2)
self.label6 = Label(self, text = "Inductance: ")
self.label6.grid(sticky = 'e', column = 0, row = 3)
#uH = unicode('�H', 'utf-8') # This works in Windows, not Linux
uH = u'\xb5H' # This works in Linux & Windows
self.label7 = Label(self, text = uH, width = 5)
self.label7.grid(sticky = 'w', column = 2, row = 3)
# Input box labels 2nd column
self.label8 = Label(self, text = "Req'd former OD: ")
self.label8.grid(sticky = 'e', column = 3, row = 0)
self.label9 = Label(self, text = "mm")
self.label9.grid(sticky = 'w', column = 5, row = 0)
self.label10 = Label(self, text = "Req'd wire length: ")
self.label10.grid(sticky = 'e', column = 3, row = 1)
self.label11 = Label(self, text = "m")
self.label11.grid(sticky = 'w', column = 5, row = 1)
self.label12 = Label(self, text = "20-220pF low end: ")
self.label12.grid(sticky = 'e', column = 3, row = 2)
self.label13 = Label(self, text = "kHz")
self.label13.grid(sticky = 'w', column = 5, row = 2)
self.label14 = Label(self, text = "20-220pF high end: ")
self.label14.grid(sticky = 'e', column = 3, row = 3)
self.label15 = Label(self, text = "kHz")
self.label15.grid(sticky = 'w', column = 5, row = 3)
# Input boxes 1st column
self.entfid = Entry(self, textvariable = self.fid, width = 10, justify = 'right')
self.entfid.grid(sticky = 'w', column = 1, row = 0)
self.entwd = Entry(self, textvariable = self.wd, width = 10, justify = 'right')
self.entwd.grid(sticky = 'w', column = 1, row = 1)
self.entnt = Entry(self, textvariable = self.nt, width = 10, justify = 'right')
self.entnt.grid(sticky = 'w', column = 1, row = 2)
self.entind = Entry(self, textvariable = self.ind, width = 10, justify = 'right', state = 'readonly')
self.entind.grid(stick = 'w', column = 1, row = 3)
# Input boxes 2nd column
self.entfid = Entry(self, textvariable = self.fod, width = 10, justify = 'right', state = 'readonly')
self.entfid.grid(sticky = 'w', column = 4, row = 0)
self.entwd = Entry(self, textvariable = self.wl, width = 10, justify = 'right', state = 'readonly')
self.entwd.grid(sticky = 'w', column = 4, row = 1)
self.entnt = Entry(self, textvariable = self.minf, width = 10, justify = 'right', state = 'readonly')
self.entnt.grid(sticky = 'w', column = 4, row = 2)
self.entind = Entry(self, textvariable = self.maxf, width = 10, justify = 'right', state = 'readonly')
self.entind.grid(stick = 'w', column = 4, row = 3)
# The picture of the former - just won't go in - or stay in - its own cell
#self.picture = Label(image = self.pic, bg = 'green', anchor = 'nw')
#self.picture.grid(column = 0, columnspan = 5, row = 5, sticky = 'nsew', ipadx = 5, ipady = 5)
# The 'Calculate' button
self.calcButton = Button(self, text='Calculate', padx = 5, command= self.calculate)
self.calcButton.grid(column = 1, columnspan = 2, rowspan = 2, row = 4, padx = 5, pady = 5)
# The 'Quit' button
self.quitButton = Button(self, text='Quit', padx = 10, command=top.destroy)
self.quitButton.grid(column = 4, columnspan = 2, rowspan = 2, row = 4, padx = 5, pady = 5, sticky = 'e')
# Bind the input of the former ID to auto-calculate - not working properly
# self.entfid.bind('<KeyPress>', self.calculate)
# Set an initial value based on the initial dimensions
self.calculate() # initialise the inductance figure
def calculate(self, arg = None): # Arg = None is for the bind command when used
modif = 25.4 # Make this 1 to calculate in inches
coilSpread = self.wd.get() * self.nt.get() # Width of the coil
meanRad = (self.fid.get() / 2) + (coilSpread / 2) # Average radius of the coil
inductance = ((meanRad * self.nt.get() / modif) ** 2) / ((8 * meanRad / modif) + (11 * coilSpread / modif))
ind = str(inductance)
ind = Decimal(ind).quantize(Decimal('0.01'), rounding = ROUND_UP)
minf = str(1 / (2 * pi * sqrt(inductance * 220 * 0.01))*100000)
minf = Decimal(minf).quantize(Decimal('0.01'), rounding = ROUND_UP)
maxf = str(1 / (2 * pi * sqrt(inductance * 20 * 0.01))*100000)
maxf = Decimal(maxf).quantize(Decimal('0.01'), rounding = ROUND_UP)
wireLength = str(2 * pi * meanRad * self.nt.get() / 1000)
wireLength = Decimal(wireLength).quantize(Decimal('0.01'), rounding = ROUND_UP)
# Set the values
self.ind.set(ind) # This will be rounded to 2 d.p.
self.fod.set(coilSpread * 2 + self.fid.get() + 5)
self.wl.set(wireLength)
self.minf.set(minf)
self.maxf.set(maxf)
def about(self):
top = Toplevel()
top.title("About Coil Calculator")
msg = Message(top, text="Spider Coil Calculator is based on the\n'Professor Coyle' spreadsheet written by Dan Petersen.\n", width = 200)
msg.grid()
button = Button(top, text="Dismiss", command=top.destroy)
button.grid()
app = Application()
app.master.title("Spider Coil Calculator")
app.mainloop()
#exit()
This post has been edited by kiwi_steve: 17 October 2010 - 04:20 PM

New Topic/Question
Reply



MultiQuote


|