import tkinter as tk
class yemian1:
def yemian(self):
root1 = tk.Tk()
root1.geometry('300x400')
self.label = tk.Label(root1)
self.one = tk.StringVar(root1)
en = tk.Entry(root1, width=80, textvariable=self.one)
button1 = tk.Button(root1, text="将小数转为二进制", command=self.cal1)
button2 = tk.Button(root1, text="由4字节整数获取颜色信息", command=self.cal2)
en.pack()
button1.pack()
button2.pack()
self.label.pack()
root1.mainloop()
def cal1(self):
i=float(self.one.get())
a1=int(i)
a2=i-a1#获取小数部分
a=''
while a1!=0:
a=a+str(a1%2)
a1=a1//2
a[::-1]#字符串翻转
b=''
n=0
while a2!=0:
t=a2*2
b=b+str(int(t))
a2=t-int(t)
n+=1
if n>=10:
break
self.label.config(text=a+'.'+b)
def cal2(self):
a=int(self.one.get())
red=(a&0x00FF0000)>>16
green=(a&0x0000FF00)>>8
blue=(a&0x000000FF)
self.label.config(text='红:'+str(red)+' 绿:'+str(green)+' 蓝:'+str(blue))
root = tk.Tk()
but1 = tk.Button(root, command=yemian1().yemian, text='页面')
but1.pack()
root.mainloop()
Python——二进制相关代码,十进制小数转二进制
最新推荐文章于 2024-03-17 14:15:15 发布