python - 在子类python中调用父方法
问题描述
这是我的代码:
class GUI(playGame):
def __init__(self):
import tkinter as tk
home=tk.Tk()
home.title("Tic Tac Toe")
home.geometry("160x180")
w,h=6,3
self.c1r1=tk.Button(text='',width=w, height=h, command=lambda: userTurn(self.c1r1))
self.c1r1.grid(column=1,row=1)
home.mainloop()
因此,userTurn 已在父类 playGame 中定义,但是当我运行它并单击按钮 c1r1 时,我得到 NameError: name 'userTurn' is not defined
解决方案
您需要self
在函数调用中添加一个。你可能应该super()
在你的 init 中调用:
import tkinter as tk
class playGame():
def userTurn(self,foo):
pass
class GUI(playGame):
def __init__(self):
super().__init__()
home=tk.Tk()
home.title("Tic Tac Toe")
home.geometry("160x180")
w,h=6,3
self.c1r1=tk.Button(text='',width=w, height=h, command=lambda: self.userTurn(self.c1r1))
self.c1r1.grid(column=1,row=1)
home.mainloop()
推荐阅读
- python - 有什么方法可以在 App 引擎上与我的 Flask 网页一起运行脚本?
- r - 在 R Studio 的数据框中交换数据点
- android - Docusign - 无法从 android 信封中获取签名图像
- html - ID 工作时 CSS 类不可格式化
- python - 是否有更有效的方法来创建映射到按集合整数移动的字母的字母字典?
- r - 使用应用函数族对 List 中的行进行子集
- node.js - 用于测试 APQ(自动持久查询)的示例 Apollo 客户端代码
- android - launchFragmentInContainer 无法解析 Activity 的意图
- mediawiki - 如何将自动完成属性添加到 MediaWiki 登录表单的输入字段?
- python - 将静态路径传递给包含的模板