如何在本地增加全局变量的数量?

发布于 2024-11-03 13:43:15 字数 409 浏览 2 评论 0原文

我正在使用 PyQt 编写一个程序,并且试图找出一种方法,以便在每次单击特定按钮时不断添加到全局变量。这样,如果我的全局值是 0,那么第三次单击按钮时,我的全局值将等于 3。这是相关代码。

# from the main window class

QtCore.QObject.connect(self.pushButton_17, QtCore.SIGNAL("clicked()"), self.CadDraw)

def CadDraw(self):
    myGlobal +=1 #the previous value from the last time CadDraw was called plus 1
    DoStuff()

这在 C 语言中相当简单,但我对 python 方法完全空白,我似乎记得它非常相似。

I'm writing a program using PyQt and I'm trying to figure out a way to keep adding to a global variable every time a particular button is clicked. This way if my global is 0 then the third time the button is clicked my global would equal 3. Here's the relevant code.

# from the main window class

QtCore.QObject.connect(self.pushButton_17, QtCore.SIGNAL("clicked()"), self.CadDraw)

def CadDraw(self):
    myGlobal +=1 #the previous value from the last time CadDraw was called plus 1
    DoStuff()

This is pretty easy in C but I'm completely blanking on the python method which I seem to recall was very similiar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2024-11-10 13:43:15

使用全局语句

global myGlobal
myGlobal = 0
def CadDraw(self):
    global myGlobal
    myGlobal +=1 [the previous value from the last time CadDraw was called plus 1]
    DoStuff()

use the global statement

global myGlobal
myGlobal = 0
def CadDraw(self):
    global myGlobal
    myGlobal +=1 [the previous value from the last time CadDraw was called plus 1]
    DoStuff()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文