为什么不能更新变量值并返回自我功能(*args)?

2024-10-03 04:40:06 发布

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

我从programa1发送一个新对象的变量值,使用:

def send_price(self):
    self.pricesend = float(self.text1.get()) #this take a value from a tkinker.Entry
    print(self.pricesend)
    objetoprograma1.Object(self.pricesend)

对象“objetoprograma1”使用以下方法返回一个新值:

^{pr2}$

现在我想更新principal1tkinter.Entry中名为self.text1的值:

def recibe_newprice(self,  new_price):

    self.new_price = new_price
    print("price new recibe" , self.new_price)

    ## this don't work.. this don't update or change the value in the tkinter.Entry

    self.text1.delete(0, len(self.text1.get()))
    self.text1.insert(self.my_main, str(self.new_price))

我有以下例外:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "B:\MAESTRIA\PYTHON\trabajos\hello\programa1.py", line 38, in send_price
    objetoprograma1.Object(self.pricesend)
  File "B:\MAESTRIA\PYTHON\trabajos\hello\objetoprograma1.py", line 19, in __init__
    programa1.Aplication.recibe_newprice(self, float(self.new_price))
  File "B:\MAESTRIA\PYTHON\trabajos\hello\programa1.py", line 51, in recibe_newprice
    self.text1.delete(self.my_main, len(self.text1.get()))
AttributeError: 'Object' object has no attribute 'text1'

完整的programa1.py

# -*- coding: latin-1 -*-
import tkinter
import objetoprograma1
import time

class Aplication():
    def __init__(self,my_main):
        self.my_main = my_main
        self.variables()
        self.GUI()

    def variables (self):
        self.price = None
        self.list = []

    def GUI(self):

        self.text1 = tkinter.Entry()
        self.text1.insert(0, "1000")
        self.text1.grid(column = 0, row = 0)

        self.boton1  = tkinter.Button(self.my_main, text = "sendprice", command = self.send_price )
        self.boton1.grid(column=1, row = 0)

    def send_price(self):
        self.pricesend = float(self.text1.get())
        print(self.pricesend)
        objetoprograma1.Object(self.pricesend)

    def recibe_newprice(self,  new_price):

        self.new_price = new_price
        print("price new recibe" , self.new_price)

        ## this don't work

        self.text1.delete(0, len(self.text1.get()))
        self.text1.insert(self.my_main, str(self.new_price))


if __name__ == "__main__":
    root = tkinter.Tk()
     #root.geometry("800x500+0+0")
    root.title("titulo")
    app = Aplication(my_main=root)
    root.mainloop()

objetoprograma1.py

# -*- coding: latin-1 -*-
import programa1
import tkinter
import time

class Object():

    def __init__(self, price):

        self.price_recibe = float(price)
        print(self.price_recibe)

        self.new_price = self.price_recibe + 10
        print(self.new_price)

        programa1.Aplication.recibe_newprice(self, float(self.new_price))

Tags: inpyselfnewmaintkintermydef
1条回答
网友
1楼 · 发布于 2024-10-03 04:40:06

查看对象类并查看异常消息。您调用的是recibe_newprice方法,但传递给它的是Object实例(对象没有text1属性)。recibe_newprice是为Aplication类编写的,因此期望self是{}类的一个实例。您似乎混淆了什么是类,或者self参数是如何工作的。在

我的第一个建议是用更具描述性的名称来命名事物。像ObjectApplication、和{}这样的名称不会告诉读者这些对象的用途。在

第二,你知道类和函数之间的区别吗?也许这会有帮助。我可以这样编写send_price方法:

def send_price(self, price_recibe):
    pricesend = float(self.text1.get())
    print(pricesend)
    print(price_recibe)

    new_price = price_recibe + 10
    print(new_price)
    self.recibe_newprice(new_price)

如果我这样做是没有道理的,或者为什么我认为这样做比你做的更好/更简单,那么我建议研究python类、属性赋值和参数传递是如何工作的。在

相关问题 更多 >