Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
这里有一些要打开包装。
首先,PACK方法返回
无
,因此当您执行时:Label
基本上没有一个,因此更改其映像将无法正常工作。其次,而不是为
paris
编写功能,为什么不编写将城市作为参数的通用函数?然后,您可以使用lambda函数将此函数分配为按钮的命令。最后,正如此答案 Image1 将在功能结束时收集,因此,您应该指定使用
global
内在功能内已声明的变量。 (更好的解决方案是将您的窗口创建为类,并设置self.image1
)There is a bit to unpack here.
First, the pack method returns
None
, so when you do:label
is basically None so changing its image won't work.Secondly, instead of writing a function for
Paris
why don't you write a generic function that will take a City as argument? You can then assign this function as command for your buttons using a lambda function.Finally, as mentionned in this answer
image1
would be garbaged collected at the end of your function, so you should specify that you're using the already declared variable inside your function withglobal
. (the better solution would be to create your window as a class, and setself.image1
)我认为问题是您的名字惯例。
您可以使用名称为“巴黎”的功能创建一个函数,但随后用您创建的类覆盖变量。
然后,当您将“巴黎”作为按钮的命令传递时,该按钮试图执行“巴黎”,但巴黎是无法执行的对象。这也是您没有错误的原因。
另外,要访问“文本”,就像您想在现在称为函数“ changeparis”中执行的“文本”,您需要使“更改”一种“城市”的方法。那就是为什么您需要将“自我”作为一个论点。
最后但并非最不重要的一点是,您需要将标签的参考等转移到“城市”中,否则您将无法访问它们。
那是我的最终代码:
我尽了最大的努力,但无法测试。请注意。希望我能帮助您!
I think the problem is your name convention.
You create a function with name "Paris" but then overwrite the variable with a class that you create.
Then, when you pass "Paris" as a command for the button, the button tries to execute "Paris", but Paris is an object which can't be executed. That's also the reason why you don't get an error.
Also, to access "text" like you want to do in your now called function "changeParis", you'd need to make "change" a method of "City". Thats, why you need to pass "self" as an argument.
Last but not least, you need to pass your references of your labels etc. into "City" otherwise, you can't access them.
That's my final code:
I tried my best, but couldn't test it. Please be aware of this. I hope I could help you!