So I have started to write this app using Tkinter, and I have somethings working. But as I start to add more measurements to my app I am finding it rather monotonous, which makes me feel like I am doing it wrong.
Here is what I have so far:
- Code: Select all
from Tkinter import *
Main window layout
def __init__(self, root_window):
self.button_quit = Button(root_window, text="Quit", fg="red", command=root_window.quit)
self.button_quit.grid(row=0, column=1, sticky=W)
self.speed_mbits_label = Label(root_window, text="Mbps")
self.speed_mbits_label.grid(row=1, column=0, sticky=W)
self.speed_mbits = Entry(root_window, validate="focusout", validatecommand=self.speed_convert)
self.speed_mbits.grid(row=1, column=1, sticky=W)
self.speed_mbytes_label = Label(root_window, text="MBps")
self.speed_mbytes_label.grid(row=1, column=3, sticky=W)
self.speed_mbytes = Entry(root_window, validate="focusout", validatecommand=self.speed_convert)
self.speed_mbytes.grid(row=1, column=4, sticky=W)
self.button_convert = Button(root_window, text="Speed Convert", fg="blue", command=self.speed_convert)
self.button_convert.grid(row=3, column=1, sticky=W)
self.button_clear = Button(root_window, text="Speed Clear", fg="black", command=self.speed_clear)
self.button_clear.grid(row=3, column=2, sticky=W)
self.size_bits_label = Label(root_window, text="bits")
self.size_bits_label.grid(row=4, column=1, sticky=W)
self.size_bits = Entry(root_window, validate="focusout", validatecommand=self.size_convert)
self.size_bits.grid(row=4, column=2, sticky=W)
Based on input into Entry fields calculate either MBps or mbps
if int(self.speed_mbits.get()) > 0:
self.speed_mbytes.insert(0, int(self.speed_mbits.get()) / 8)
elif int(self.speed_mbytes.get()) > 0:
self.speed_mbits.insert(0, int(self.speed_mbytes.get()) * 8)
Reset Entry fields for speed back to 0
root_window = Tk()
app = Conversion(root_window)
While I still have a problem with the focusout working after I have used it once, and I need to make it a little more user friendly. My problem is I am repeating the same code over and over again (Label, Entry, Grid, Insert).
I feel like I should create a dictionary with a tuple to build the root_window rather than copying and pasting my code over and over. But if I were to create a dictionary with tuples, then I would be better off building another class to handle this.
I'm not really quite sure how I would handle this.
My main hang up is the self.variables. I don't know how I would loop the self.speed_mbits_label.
Or how I would handle the setup of the grid. Maybe I would create a newline variable in a tuple that added 1 to the row, and then have +1 to the column for each item created.
I'm not necessarily stuck, as I can continue on, but I think that my code is bloated and I just want to find a better way of doing it.