Python——二进制相关代码,十进制小数转二进制

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bhZ567

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值