Answer the question
In order to leave comments, you need to log in
I don’t understand why it doesn’t display the value of the described function in the inscription?
from tkinter import *
sNumSystem = 0
fNumSystem = 0
def click2(event):
sNumSystem = 2
def click8(event):
sNumSystem = 8
def click10(event):
sNumSystem = 10
def click16(event):
sNumSystem = 16
def click2f(event ):
fNumSystem = 2
def click8f(event):
fNumSystem = 8
def click10f(event):
fNumSystem = 10
def click16f(event):
fNumSystem = 16
# Binary conversion function
def ConvertFromBin(_value, _numSystem):
if _numSystem == 2:
return _value
elif _numSystem == 8:
try:
return oct(int(_value, 2))
except ValueError:
print('there are only 1 and 0 in binary system')
elif _numSystem == 10:
try:
return int(_value, 2)
except ValueError:
print('in in binary there are only 1 and 0')
elif _numSystem == 16:
try:
return hex(int(_value, 2))
except ValueError:
print('in binary there are only 1 and 0')
# Transfer function from octal system
def ConvertFromOct(_value, _numSystem):
if _numSystem == 2:
try:
return bin(int(_value, 8))
except ValueError:
print('In octal there are only numbers from 0 to 7')
elif _numSystem == 8:
try:
return _value
except ValueError:
print('In octal there are only numbers 0 to 7')
elif _numSystem == 10:
try:
return int(_value, 8)
except ValueError:
print('there are only digits 0 to 7 in octal')
elif _numSystem == 16:
try:
return hex( int(_value, 8))
except ValueError:
print('In octal there are only digits from 0 to 7')
# Decimal conversion function
def ConvertFromDec(_value, _numSystem):
if _numSystem == 2:
try:
return bin(int(_value))
except ValueError:
print( 'there are only digits from 0 to 9 in decimal')
elif _numSystem == 8:
try:
return oct(int(_value))
except ValueError:
print('there are only digits from 0 to 9 in decimal')
elif _numSystem = = 10:
try:
return int(_value)
except ValueError:
print('in decimal only digits from 0 to 9')
elif _numSystem == 16:
try:
return hex(int(_value))
except ValueError:
print('in decimal only digits from 0 to 9')
# Hex conversion function
def ConvertFromHex(_value, _numSystem):
if _numSystem == 2:
try:
return bin(int(_value, 16))
except ValueError:
print('in hex there are only digits from 0 to A')
elif _numSystem == 8:
return oct(int(_value, 16))
elif _numSystem == 10:
return int(_value, 16)
elif _numSystem == 16:
return _value
def clickGO(event):
x = entryvalue.get()
if fNumSystem == 2:
answer = Label(root, text=ConvertFromBin(x, sNumSystem))
answer.grid(row=7, column=3)
elif fNumSystem == 8:
answer = Label(root, text=ConvertFromOct(x, sNumSystem))
answer.grid(row=7, column=3)
elif fNumSystem == 10:
answer = Label(root, text =ConvertFromDec (x, sNumSystem))
answer.grid(row=7, column=3)
elif fNumSystem == 16:
answer = Label(root, text=ConvertFromHex(x, sNumSystem))
answer.grid(row=7, column =3)
root = Tk()
root.title("Number Systems Calculator")
root.geometry("800x600")
root.configure(bg="white")
btn2 = Button(root, text="of 2")
btn2.grid(row=0, column =2)
btn2.bind("", click2)
btn8 = Button(root, text="of 8")
btn8.grid(row=0, column=3)
btn8.bind("", click8)
btn10 = Button( root, text="of 10")
btn10.grid(row=0, column=4)
btn10.bind("", click10)
btn16 = Button(root, text="of 16")
btn16.grid(row=0 , column=5)
btn16.bind("", click16)
btn2f = Button(root, text="to 2")
btn2f.grid(row=3, column=2)
btn2f.bind("",click2f)
btn8f = Button(root, text="at 8")
btn8f.grid(row=3, column=3)
btn8f.bind("", click8f)
btn10f = Button(root, text="at 10")
btn10f.grid(row=3, column=4)
btn10f.bind("", click10f)
btn16f = Button(root, text ="to 16")
btn16f.grid(row=3, column=5)
btn16f.bind("", click16f)
btnGO = Button(root, text="Translate")
btnGO.grid(row=4, column=5 )
btnGO.bind("", clickGO)
entryvalue = Entry(root)
entryvalue.grid(row=0, column=7)
root.mainloop()
Answer the question
In order to leave comments, you need to log in
Firstly, you have a problem with the scope:
add the line "global #NumSystem" to each def click#(event): - then the assignment will not be a local variable, but that #NumSystem, in the global namespace, which you now have everything time remains = 0.
Secondly, you got it wrong in def clickGO(event):
the request goes according to f NumSystem, and the text is formed according to ConvertFromBin(x, s NumSystem) - I think it was planned in accordance, otherwise the function returns None.
And third, in order for at least something to happen, in all btn##.bind("", click##) I added " " instead of empty quotes.
Well, in general, extreme redundancy in the code and no architecture - try not to write like that. <Button-1>
def clickGO(event):
x = entryvalue.get()
if fNumSystem == 2:
answer = Label(root, text=ConvertFromBin(x, sNumSystem))
answer.grid(row=7, column=3)
elif fNumSystem == 8:
answer = Label(root, text=ConvertFromOct(x, sNumSystem))
answer.grid(row=7, column=3)
elif fNumSystem == 10:
answer = Label(root, text=ConvertFromDec (x, sNumSystem))
answer.grid(row=7, column=3)
elif fNumSystem == 16:
answer = Label(root, text=ConvertFromHex(x, sNumSystem))
answer.grid(row=7, column=3)
in this snippet
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question