【问题标题】:Python3 - Problems with converting between different number systemsPython3 - 在不同数字系统之间转换的问题
【发布时间】:2016-08-26 06:11:30
【问题描述】:

我正在编写一个简单的程序来在基数 10、2、8 和 16 之间切换数字。但是我在多次转换或转换回拒绝数时遇到问题。这是我正在使用的代码

#! python3

import tkinter as tk
from tkinter import ttk

CurrentSystem = 'd'

def clearEnt():
    ent.delete(0, last = None)

def baseConverter(value,base):
    try:
        if base == 2:
            clearEnt()
            ent.insert(0, bin(value))
            CurrentSystem = 'b'
        if base == 8:
            clearEnt()
            ent.insert(0, oct(value))
            CurrentSystem == 'o'
        if base == 16:
            clearEnt()
            ent.insert(0, hex(value))
            CurrentSystem == 'h'
        if base == 10:
            if CurrentSystem == 'b':
                clearEnt()
                ent.insert(0, int(value, 2))
            if CurrentSystem == 'o':
                clearEnt()
                ent.insert(0, int(value, 8))
            if CurrentSystem == 'h':
                clearEnt()
                ent.insert(0, int(value, 16))
    except ValueError:
        clearEnt()
        ent.insert(0, 'ERROR')

root = tk.Tk()
root.title('Number Converter')

ent = ttk.Entry()
ent.grid (row = 0, column = 1, columnspan = 2, padx = 5)

button1 = ttk.Button(root, text = "Den")
button2 = ttk.Button(root, text = "Bin", command = lambda:baseConverter(int(ent.get()), 2))
button3 = ttk.Button(root, text = "Oct", command = lambda:baseConverter(ent.get(), 8))
button4 = ttk.Button(root, text = "Hex", command = lambda:baseConverter(ent.get(), 16))

button1.grid(row = 1, column = 0)
button2.grid(row = 1, column = 1)
button3.grid(row = 1, column = 2)
button4.grid(row = 1, column = 3)

root.mainloop()

如您所见,这个想法是 ent TK 输入框中的数字会根据按钮按下而变化。到目前为止,它将从 den 更改为另一个系统,然后拒绝更改或再次工作。我将不胜感激任何帮助或建议。谢谢

【问题讨论】:

  • 让你的代码模块化。暂时完全忘记 tkinter,只专注于获得正确的转换功能。然后 - 在您的事件处理程序中适当地调用它们。转换本身有什么问题?
  • 使用 python3.4 运行时,会不断出现错误消息,例如:ent.insert(0, oct(value)) TypeError: 'str' object cannot be Explained as an integer。最有可能的问题是值被表示为字符串并且您的 oct/hex 命令失败。
  • 为了扩展我的最后一条评论,一个好的策略是编写一个 Python 函数 changeBase(s,a,b),它需要一个 string s,一个 int a表示 s 的当前基数和一个表示目标基数的 int b,它返回一个表示目标基数中的数字的字符串。得到它以便它在 shell 中完美运行(也许为它编写一个测试脚本来测试所有 4x4 = 16 个有效案例),然后担心如何将它连接到 tkinter

标签: python python-3.x tkinter binary converter


【解决方案1】:

这由两部分组成,从字符串到整数,然后再返回。因此,为每个功能单元创建一个函数是个好主意。

将字符串转换为整数。

int('100', 8) == 8**2  # second argument is the base

将整数转换为字符串。

def int_to_str(number, base):
    assert isinstance(number, int)
    char = {2:'b', 8:'o', 10:'d', 16:'x'}[base]
    return '{0:{1}}'.format(number, char)

int_to_str(8**2, 8) == '100'

然后在您的 TK 代码中使用这些函数。像这样的:

button1 = ttk.Button(root, text="Dec", command=lambda:int(ent.get(), 10))
button2 = ttk.Button(root, text="Bin", command=lambda:int(ent.get(), 2))
button3 = ttk.Button(root, text="Oct", command=lambda:int(ent.get(), 8))
button4 = ttk.Button(root, text="Hex", command=lambda:int(ent.get(), 16))

【讨论】:

  • 您的意思是我使用第一行作为 lambda,然后在打印之前将它们放入函数中?
  • 从输入框中得到的是一个字符串。要将其转换为整数,您需要知道基数。您的代码表明用户通过按下相应的按钮告诉您基地。因此,您使用int 函数,将输入字符串和基数都传递给它,以获取用户想要的实际整数。要获得输出数字的基础,您需要来自例如的第三条信息。一个 TK 单选按钮。然后调用int_to_str,使用要转换的整数(之前int 的结果)和输出基数,并向用户显示结果字符串。
【解决方案2】:

注意 4 行缺乏对称性:

button1 = ttk.Button(root, text = "Den")
button2 = ttk.Button(root, text = "Bin", command = lambda:baseConverter(int(ent.get()), 2))
button3 = ttk.Button(root, text = "Oct", command = lambda:baseConverter(ent.get(), 8))
button4 = ttk.Button(root, text = "Hex", command = lambda:baseConverter(ent.get(), 16))

在第一个中,没有命令,第二个有意义(但仍然有问题),第三个和第四个缺少对第四个中的int 的调用。为什么不让所有 4 行看起来更像第二行呢?另外——请注意,如果int() 采用可选参数作为基数。也许你想对这个论点做点什么,涉及到CurrentSystem。请注意,为此目的,CurrentSystem 可能会更好地保存诸如 2、8、10 或 16 之类的 int 而不是字符串。

最后,请注意,出于显示目的,您可能希望显示类似于100101 而不是0b100101 的内容。为此,您可以使用切片运算符: if s = '0b100101' then s[2:] = '100101'

【讨论】:

  • 我完全忽略了按钮 2 和 3/4 之间的区别。我做了这个改变。我真的不知道第一个按钮该怎么做
  • 您可能对缺少 base-10 版本的 bin, oct, hex 感到困惑。在这种情况下只需使用str()。 Base-10 是int默认 字符串表示。你不需要做任何特别的事情。另外,请注意int("123",10) = 123)。没有必要提供基数 (10) - 但它也不会伤害任何东西。
  • 如果我转换为一个系统并希望它回到 den 怎么办?我将添加单选按钮以显示条目中的值使用的当前数字系统
  • 所有的转换都应该是从 string 到 int 再回到 string。使用 int 作为公分母。从 8 基到 10 基应该不会比从 8 基到 16 基更难。
  • ghostbin.com/paste/wcjjz 这是代码现在的样子。我勾选 den 并按下 oct 按钮。相反,我收到此错误prntscr.com/az54hc
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 2017-07-12
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多