Python 为什么我能';是否更新变量值并返回self.func(*args)?
我正在从Python 为什么我能';是否更新变量值并返回self.func(*args)?,python,tkinter,Python,Tkinter,我正在从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”使用以下命令返回新值: class Object(
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”使用以下命令返回新值:
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))
现在我想更新名为self.text1的principal1
tkinter.Entry
中的值:
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()
# -*- 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))
和objetoprograma1.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()
# -*- 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))
查看对象类并查看异常消息。您正在调用recibe\u newprice
方法,但将对象
实例传递给它(对象没有text1属性)。recibe\u newprice
是为application
类编写的,因此期望self
成为application
类的一个实例。您似乎混淆了类的用途或self
参数的工作方式
我的第一个建议是用更具描述性的名称来命名事物。像对象
、应用程序
和程序1
这样的名称不会告诉读者这些对象的用途
第二,你知道类和函数之间的区别吗?也许这会有帮助。我将按以下方式对send\u 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类、属性赋值和参数传递的工作原理。daveydave400先生。。。谢谢你的回答。。。。但这不起作用。。。。无论如何,我不想也不需要加入这两个文件。。。。我有另一个程序与更多的代码和相同的问题。。。也许你有其他的想法来解决这个问题吗??thanks@yhoyo-我认为daveydave400建议了一种更好的方法来实现您的一个方法,但老实说,您需要重新思考、重新设计并重新编写所有代码,因为您似乎完全误解了对象、方法和函数的作用。@yhoyo,@TonySuffolk66是正确的。我本想表现得很好,但你似乎误解了争论是如何传递的;特别是当涉及到类/对象和self
参数时(该参数随类的实例自动传递)。我之所以重写该方法,是因为您使用类的方式(特别是对象
类)是错误的。课程不是那样的。@daveydave400和TonySuffolk66谢谢你的回答。。。经过一番研究,我明白了你为什么告诉我这件事。。。。谢谢大家