如何从类创建的条目中获取文本

2024-04-26 08:10:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习python,并决定用OOP和Tkinter创建一个统一线条移动的计算器。因为我需要几个输入框,所以我为它创建了一个类,但是在提取文本时我遇到了错误。代码如下:

from tkinter import *

root = Tk()  # Crea una ventana basica. SIEMPRE se debe utilizar
root.geometry('350x200')  # Setea las dimensiones de la ventana
root.title("Simple MRU Calculator.")


class labels:  # Crea labels de texto
    def __init__(self, texto, fuente, tamañoFuente, columna, rowa):
        self = Label(root, text=texto, font=(fuente, tamañoFuente)
                     ).grid(column=columna, row=rowa)


class entryBox:  # Crea entryboxes
    def __init__(self, largo, columna, rowa):
        self = Entry(root, width=largo)
        self.grid(column=columna, row=rowa)


entryBox_Distancia = entryBox(10, 1, 0)
entryBox_Velocidad = entryBox(10, 1, 1)
entryBox_TiempoF = entryBox(10, 1, 2)
entryBox_TiempoI = entryBox(10, 1, 3)


def calculando():
    entryBoxDT = entryBox_Distancia.get()
    print(entryBoxDT)


theLabel_Uno = labels('Distancia Inicial:', 'Arial', 11, 0, 0)
theLabel_Dos = labels('Velocidad', 'Arial', 11, 0, 1)
theLabel_Tres = labels('Tiempo Final', 'Arial', 11, 0, 2)
theLabel_Cuatro = labels('Tiempo Inicial', 'Arial', 11, 0, 3)

boton_Uno = Button(root, text='Calcular', command=calculando)
boton_Uno.grid(column=1, row=5)
theLabel_Cinco = labels('n', 'Arial', 11, 1, 4)


root.mainloop()  # Inicia la ventana

我得到的错误是: AttributeError: 'entryBox' object has no attribute 'get'

如何从每个输入框中获取文本?你知道吗


Tags: selflabelsdefcolumnrootgridrowcrea
1条回答
网友
1楼 · 发布于 2024-04-26 08:10:17

您的问题是您没有从entry小部件继承。简单地说self = whatever没有任何作用。要完成此任务,请使用以下命令:

class entryBox(Entry):  # inherit from the entry widget
    def __init__(self, largo, columna, rowa):
        super().__init__(width=largo)  # call the init method of the entry widget.
        self.grid(column=columna, row=rowa)

现在entryBox是entry小部件的实际实例。你知道吗

This是在python中处理继承的一个很好的教程。你知道吗

而且this更深入地探讨了为什么重新定义自我是行不通的。你知道吗

相关问题 更多 >