Tkinter的进阶版本:同样的代码,为何小甲鱼视频里可以运行,我的电脑上却不行?
WIN10,python版本3.7.6----------------------------------
import tkinter as tk
class APP:
def __init__(self, master):
frame = tk.Frame(master)
frame.pack()
self.hi_there = tk.Button(frame, text='打招呼', fg='blue')
self.hi_there.pack()
#def say_hi(self):
#print('互联网的广大朋友们,大家好!我是你们的小甲鱼哦…')
root = tk.TK()
app = APP(root)
root.mainloop()
无法正常运行,提示如下:
Traceback (most recent call last):
File "D:\work\p15_2.py", line 20, in <module>
root = tk.TK()
AttributeError: module 'tkinter' has no attribute 'TK'
不知道什么情况?代码完全按照小甲鱼视频,视频里却可以运行
Tk的 k 是小写的,参考代码:
import tkinter as tk
class APP:
def __init__(self, master):
frame = tk.Frame(master)
frame.pack()
self.hi_there = tk.Button(frame, text='打招呼', fg='blue')
self.hi_there.pack()
# def say_hi(self):
# print('互联网的广大朋友们,大家好!我是你们的小甲鱼哦…')
root = tk.Tk()
app = APP(root)
root.mainloop()
自己发现问题了:
root = tk.TK() 应改为 root = tk.Tk()
不过新问题又来了:为何必须Tk才可以而TK不行? lzb1001 发表于 2022-5-10 17:14
自己发现问题了:
root = tk.TK() 应改为 root = tk.Tk()
就是没有那个方法 lzb1001 发表于 2022-5-10 17:14
自己发现问题了:
root = tk.TK() 应改为 root = tk.Tk()
可以的这样:
import tkinter as tk
class TK(tk.Tk):
pass
class APP:
def __init__(self, master):
frame = tk.Frame(master)
frame.pack()
self.hi_there = tk.Button(frame, text='打招呼', fg='blue')
self.hi_there.pack()
# def say_hi(self):
# print('互联网的广大朋友们,大家好!我是你们的小甲鱼哦…')
root = TK()
app = APP(root)
root.mainloop()
页:
[1]