- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在使用 Python 编写自己的类时,我还比较陌生。我在一个程序中工作,在这个程序中我已经定义了 3 个类,每个类有 4/5 个方法,每个类都可以正常工作。但是我在一个小时内遇到了一个新问题,我无法弄清楚我做错了什么。事实上,即使没有弹出错误消息,该方法也不起作用,但如果我在我的 pygame 循环(调用它的地方)中复制粘贴该方法的代码,它就可以正常工作。这是我类(class)的代码(.click(self) 方法是这里的关键之一):
class button:
def __init__(self, window, xpos, ypos, width, height, color = white, text = '', dest = None):
self.xpos = xpos * r
self.ypos = ypos * r
self.width = width * r
self.height = height * r
self.color = color
self.colorBuffer = [color[i] for i in range(3)]
self.window = window
self.hovered = False
self.clicked = False
self.text = text
self.dest = dest
#self.R = color[0]
#self.G = color[1]
#self.B = color[2]
def click(self):
self.clicked = True
if self.dest == None:
pass
else:
currentMenu = self.dest
self.dest.disp()
self.clicked = False
这是调用方法的代码(当程序检查 event.type == MOUSEBUTTONDOWN 时调用):
loop = False
if loop == False:
currentMenu = mainMenu
loop = True
boxisinputting = False
boxinputting = None
userisinputting = False
while loop == True:
pygame.display.flip()
userisinputting = False
mousex, mousey = pygame.mouse.get_pos()
currentMenu.disp()
for event in pygame.event.get():
if event.type == pygame.QUIT:
loop = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if pygame.key.name(event.key) == 'return':
boxinputting.inputting = False
boxisinputting = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
for but in currentMenu.buttonList:
if but.hovered == True:
but.click()
if boxisinputting == False:
for box in currentMenu.inputBoxList:
if box.hovered == True:
box.inputting = True
boxisinputting = True
boxinputting = box
if userisinputting == True:
boxinputting.render()
pygame.time.delay(100)
当我运行这段代码时会发生什么:下一个菜单显示正常,但它的按钮没有响应(无论鼠标是否在按钮上,我都有点亮或熄灭它的方法);相反,当我将指针移到它们所在的位置时,弹出的是上一个菜单的按钮。但是,当我复制粘贴指令时,我希望我的方法 .click(self) 在这里执行:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
for but in currentMenu.buttonList:
if but.hovered == True:
but.clicked = True
if but.dest == None:
pass
else:
currentMenu = None
currentMenu = but.dest
but.dest.disp()
but.clicked = False
... 好吧,它工作得非常好:按钮处于事件状态并且有响应,并且之前的按钮不会凭空弹出(这是我愿意做的)。我可以摆脱它,因为这只会在我的循环中浪费 5 或 6 行。但我很想知道为什么我无法使该方法起作用,以避免将来出现此类问题。
感谢您阅读我的帖子!
威廉
最佳答案
这很难说,因为包含的代码没有显示 button
的实例化,但您可能需要确保它引用了全局范围:
def click(self):
global currentMenu, gamesMenu
currentMenu = gamesMenu
print('click')
如果它只是仅一个按钮,您的按钮类会更好。所以这与位置、标签、颜色、是否被点击、未点击、悬停等有关。但不是也很重要。按钮不是菜单,它不应该知道菜单,也不应该处理菜单(或刷新显示缓冲区!)。
当对象的行为简洁明了时,它们会产生更好的设计和更清晰的代码。
关于python - 我无法使类方法起作用;如果我将方法代码复制粘贴到应该调用该方法的位置,它就可以工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65098860/
我正在编写一个应用程序,允许用户创建一个“问卷”,然后向其中添加问题。我正在使用核心数据来存储信息。我创建了一个问卷实体,并与问题实体建立了“一对多”关系。我的问题是,如果要允许用户复制(复制)整个调
有没有办法复制或复制 SharedPreference?或者我需要从一个变量中获取每个变量,然后将它们放入另一个变量中吗? 最佳答案 尝试这样的事情: //sp1 is the shared pref
下面的(A)和(B)有区别吗? (假设 NON ARC,如果重要的话) // --- (A) --- @interface Zoo : NSObject{} @property (copy) Dog
我正在尝试将 mysql SELECT 查询保存到文件中,如下所示: $result = mysqli_query($db,$sql); $out = fopen('tmp/csv.csv', 'w'
我需要创建一个 CVPixelBufferRef 的副本,以便能够使用副本中的值以按位方式操作原始像素缓冲区。我似乎无法使用 CVPixelBufferCreate 或 CVPixelBufferCr
我在 Source 文件夹中有一个 Active wave 录音 wave-file.wav。我需要使用新名称 wave-file-copy.wav 将此文件复制到 Destination 文件夹。
在使用 GNU Autotools 构建的项目中,我有一个脚本需要通过 make 修改以包含安装路径。这是一个小例子: configure.ac: AC_INIT(foobar, 1.0) AC_PR
我想将 SQL 的行复制到同一个表中。但是在我的表中,我有一个“文本”列。 使用此 SQL: CREATE TEMPORARY TABLE produit2 ENGINE=MEMORY SELECT
谁能给我解释一下 df2 = df1 df2 = df1.copy() df3 = df1.copy(deep=False) 我已经尝试了所有选项并执行了以下操作: df1 = pd.DataFram
Hazelcast 是否具有类似于 Ehcache 的复制? http://www.ehcache.org/generated/2.9.0/pdf/Ehcache_Replication_Guide.
我有以下拓扑。一个 Ubuntu 16.04。运行我的全局 MySQL 服务器的 Amazon AWS 上的实例。我想将此服务器用作许多本地主服务器(Windows 机器 MySQL 服务器)的从服务
使用 SQLyog,我正在测试表中是否设置了正确的值。我尝试过 SELECT type_service FROM service WHERE email='test@gmail.com' 因此,只输出
有人可以提供一些关于如何配置 ElasticSearch 进行复制的说明。我在 Windows 中运行 ES,并且了解如果我在同一台服务器上多次运行 bat 文件,则会启动一个单独的 ES 实例,并且
一 点睛 ThreadGroup 复制线程的两个方法。 public int enumerate(Thread list[]) // 会将 ThreadGroup 中的 active 线程全部复制到
一 点睛 ThreadGroup 复制线程组的两个方法。 public int enumerate(ThreadGroup list[]) // 相对于 enumerate(list,true) pu
官方documentation Cassandra 说: Configure the keyspace and create the new datacenter: Use ALTER KEYSPAC
This question already has answers here: How to weight smoothing by arbitrary factor in ggplot2? (2个答
我们有一个表格来表明对各种俱乐部的兴趣。输出将数据记录在 Excel 电子表格中,其中列有他们的首选姓名、姓氏、电子邮件、代词,以及他们感兴趣的俱乐部的相应列中的“1”(下面的模型)。 我们希望为俱乐
This question already has answers here: Closed 8 years ago. Possible Duplicate: In vim, how do I get
如何复制形状及其所在的单元格?当我手动复制时,形状会跟随单元格,但是当我使用宏进行复制时,我会得到除形状之外的所有其他内容。 Cells(sourceRow, sourceColumn).Copy C
我是一名优秀的程序员,十分优秀!