python - 在另一个类中调用类方法 - Tkinter
问题描述
我在 Python 3 中使用 Tkinter 在十六进制、二进制和十进制之间创建转换器 - 同时创建我自己的转换方法而不是使用内置方法。
我已经通过在 DenToBin 类中使用我的“highestPowerOf2”方法完成了从拒绝到二进制的转换。我想在我的 HexToBin 类中使用此方法,但每当我尝试使用时,都会弹出一个额外的空白窗口并且我收到 - AttributeError: 'NoneType' object has no attribute 'title' - 错误。
我将如何在 HexToBin 类中运行“highestPowerOf2”?
(也对任何糟糕的编程实践感到抱歉)
代码:
from tkinter import *
from functools import partial
class Menu(Frame):
def __init__(self, master= None): # initialise Menu
Frame.__init__(self, master) # initialise Frame
self.master = master # what does this do
self.createWindow()
def createWindow(self):
self.pack(fill = BOTH, expand = 1)
denToBin = Button(self, text = 'Denary to Binary', command = lambda: self.changeWindow(DenToBin))
denToBin.pack(fill = X, padx = 10, pady = 10)
hexToBin = Button(self, text = 'Hex to Binary', command = lambda: self.changeWindow(HexToBin))
hexToBin.pack(fill = X, padx = 10, pady = 10)
def changeWindow(self, object):
root.withdraw()
currentFrame = object(root)
class DenToBin(Toplevel):
def __init__(self, master = None):
Toplevel.__init__(self, master)
self.master = master
self.Window()
self.denary = 0
def Window(self):
# print(self.denary) -- Why is this not recognised?????????
self.master.title('Unit Converter: Denary to Binary')
instructionDen = Label(self, text = 'Denary Value: ')
instructionDen.grid(row = 1, column = 0, padx = 10)
instructionBin = Label(self, text = 'Binary Value: ')
instructionBin.grid(row = 2, column = 0, padx = 10)
self.denaryEntry = Entry(self)
self.denaryEntry.grid(row = 1, column = 2, padx = 10, pady = 5)
convertButton = Button(self, text = 'Convert!', command = lambda: self.highestPowerOf2(10)) # as an example
convertButton.grid(row = 3, column = 1)
# finds highest power of 2 that is less than denary number - helps self.convert()
def highestPowerOf2(self,number):
print('I find the highest power of 2 that fits into the number given.')
class HexToBin(Toplevel):
def __init__(self, master = None):
Toplevel.__init__(self, master)
self.master = master
self.Window()
self.denary = 0
def Window(self):
self.master.title('Unit Converter: Hexadecimal to Binary')
instructionHex = Label(self, text = 'Hexadecimal Value: ')
instructionHex.grid(row = 1, column = 0, padx = 10)
instructionBin = Label(self, text = 'Binary Value: ')
instructionBin.grid(row = 2, column = 0, padx = 10)
self.hexadecimalEntry = Entry(self)
self.hexadecimalEntry.grid(row = 1, column = 2, padx = 10, pady = 5)
convertButton = Button(self, text = 'Convert!', command = self.convert)
convertButton.grid(row = 3, column = 1)
def convert(self):
# I need to call the 'highestPowerOf2' method here.
pass
root = Tk()
unitConverter = Menu(root) # root is master
root.mainloop()
解决方案
推荐阅读
- python - 有没有办法为 discord.py wait_for 使用异步检查功能?
- python - 数据框中的字符串操作
- swiftui - 我们如何在 onChange/onReceive 中使用 ObservableObject?
- ruby-on-rails - ruby 2.1.2中的“#$e”是什么意思
- python - 如何基于 yml 文件但使用不同的 Python 版本创建新的 conda env
- python-3.x - 在 __post_init__ 中设置可选数据类参数时如何避免检查无
- javascript - 文本框输入值不变
- c - 如何在 C 中将字符串的一部分转换为不同的整数?
- arrays - 尝试使用反应钩子设置状态,但我得到的是一个空数组
- android - IntelliJ IDEA Ultimate Edition (version 2019.3) 在 Flutter Doctor 中展示