from tkinter import *
top=Tk()
top.title("进制转换")
top.geometry("300x150")
s=""
t=[]
def er(m):
if m>1:
er(m//2)
global s
s=s+str(m%2)
def ba(n):
if n>7:
ba(n//8)
global s
s=s+str(n%8)
def sl(k):
if k>15:
sl(k//16)
global t
t.append(k%16)
def b1():
global s
global t
e1=int(entry1.get())
e2=int(entry2.get())
if e2==2:
er(e1)
label3.config(text=s)
s=''
elif e2==8:
ba(e1)
label3.config(text=s)
s=''
elif e2==16:
sl(e1)
for i in t:
if i==10:
s+="A"
elif i==11:
s+="B"
elif i==12:
s+="C"
elif i==13:
s+="D"
elif i==14:
s+="E"
elif i==15:
s+="F"
else:
s+=str(i)
label3.config(text=s)
s = ""
t = []
else:
label3.config("输入错误,请重新输入")
label1=Label(top,text="请输入十进制整数:")
label1.grid(row=0,column=0)
label2=Label(top,text="请选择进制转换(2/8/16):")
label2.grid(row=1,column=0)
label3=Label(top,text="输出结果",fg='red',bg='#999999',height=3,width=20)
label3.grid(row=3,column=0,columnspan=2)
entry1=Entry(top,text='0')
entry1.grid(row=0,column=1)
entry2=Entry(top,text='1')
entry2.grid(row=1,column=1)
button1=Button(top,text="转换",command=b1)
button1.grid(row=2,column=0,columnspan=2)
top.mainloop()