patternpythonMinor
Binary/decimal/hex converter using Tkinter
Viewed 0 times
decimaltkinterhexbinaryusingconverter
Problem
I debated long and hard before posting this question, and I did a lot of experimenting. I just can't seem to work out an 'elegant', concise way to get done what I want done in the manner I want it done. I found it very hard to research because of the difficulty in not knowing exactly what to search for.
Again, I am writing a converter for binary, decimal and hex using Tkinter and not using any of Python's built-in math functions.
In the code I have one class, within it many methods. I have the methods to convert from binary to decimal and hex working correctly (
I'm going to post the entire code except for the method that creates the Tkinter widgets:
```
def base_check(self):
""" Disable Checkbox that's connected with chosen Radiobutton. """
sel_radio = self.base.get()
for radio, cb in self.cb_to_radio.items():
if radio == sel_radio:
cb.configure(state = DISABLED)
else:
cb.configure(state = NORMAL)
def conv_segue(self):
""" Decides and directs towards proper conversion method.
Reading the 'convert from' radiobuttons. """
base = self.base.get()
if base == 'bin':
bits = self.input_str.get()
# test string validity
bit_list = list(bits)
ill_bits = ['2', '3', '4', '5', '6', '7', '8', '9']
for bit in bit_list:
if bit in ill_bits:
self.output_disp.delete(0.0, END)
self.output_disp.i
Again, I am writing a converter for binary, decimal and hex using Tkinter and not using any of Python's built-in math functions.
In the code I have one class, within it many methods. I have the methods to convert from binary to decimal and hex working correctly (
bin_to_dec & bin_to_hex, respectively). I am now working on the 'from decimal' conversions. I have the dec_to_bin method working correctly, as well. My issue is with the dec_to_hex method. I want it to use the dec_to_bin method to convert the string to binary first and then use the bin_to_hex method for the final conversion. In doing this it has to overlook the lines of code that tell the 2 methods to display their results; but rather to store the results and transport them to the dec_to_hex method.I'm going to post the entire code except for the method that creates the Tkinter widgets:
```
def base_check(self):
""" Disable Checkbox that's connected with chosen Radiobutton. """
sel_radio = self.base.get()
for radio, cb in self.cb_to_radio.items():
if radio == sel_radio:
cb.configure(state = DISABLED)
else:
cb.configure(state = NORMAL)
def conv_segue(self):
""" Decides and directs towards proper conversion method.
Reading the 'convert from' radiobuttons. """
base = self.base.get()
if base == 'bin':
bits = self.input_str.get()
# test string validity
bit_list = list(bits)
ill_bits = ['2', '3', '4', '5', '6', '7', '8', '9']
for bit in bit_list:
if bit in ill_bits:
self.output_disp.delete(0.0, END)
self.output_disp.i
Solution
Use more python, e.g. instead of:
Remove extraneous keys() and it becomes:
Then remove unnecessary else and it is:
And finally remove the loop:
There I saved you a loop, a branch and 4 out of 5 lines of code.
A few iterations like this over the entire module and you might like your code after all!
for i in hex_digits.keys():
if i == tot:
tot = hex_digits[i]
else:
tot = totRemove extraneous keys() and it becomes:
for i in hex_digits:
if i == tot:
tot = hex_digits[i]
else:
tot = totThen remove unnecessary else and it is:
for i in hex_digits:
if i == tot:
tot = hex_digits[i]And finally remove the loop:
tot = hex_digits.get(i, tot)There I saved you a loop, a branch and 4 out of 5 lines of code.
A few iterations like this over the entire module and you might like your code after all!
Code Snippets
for i in hex_digits.keys():
if i == tot:
tot = hex_digits[i]
else:
tot = totfor i in hex_digits:
if i == tot:
tot = hex_digits[i]
else:
tot = totfor i in hex_digits:
if i == tot:
tot = hex_digits[i]tot = hex_digits.get(i, tot)Context
StackExchange Code Review Q#8740, answer score: 6
Revisions (0)
No revisions yet.