首页 > 解决方案 > 在另一个类中调用类方法 - 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()

标签: pythonclasstkinter

解决方案


推荐阅读