我有个问题.我认为一个类可以基于一个对象或以前定义类.当我将其更改为类Application(对象)时:它不起作用.你能告诉我为什么它不起作用,为什么下面的代码有效或为什么类应用程序(框架)有效?Frame不是先前定义的对象而不是对象.对不起我的英语不好.这是我的代码:
# Lazy Buttons
# Demonstrates using a class with tkinter
from tkinter import *
class Application(Frame): #
""" A GUI application with three buttons. """
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create three buttons that do nothing. """
# create the first Button
self.bttn1 = Button(self, text= "I do nothing.")
self.bttn1.grid()
# create second button
self.bttn2 = Button(self)
self.bttn2.grid()
self.bttn2.configure(text="Me too!")
# create third button
self.bttn3 = Button(self)
self.bttn3.grid()
self.bttn3["text"] = "Same here!"
# main
root= Tk()
root.title("Lazy Buttons")
root.geometry("200x85")
app = Application(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
Frame是以前定义的类.它是tkinter您在第一行导入的一部分.
你的Application类扩展Frame,这意味着它从中获取方法,Frame并且可以完成Tk Frame可以执行的所有操作,例如show widgets.如果你不扩展Frame,而只是扩展object,这将无效.
更换可能更清楚......
from tkinter import *
Run Code Online (Sandbox Code Playgroud)
与...
import tkinter as tk
Run Code Online (Sandbox Code Playgroud)
并修复你对Tk类的引用(它们将成为tk.Button,tk.Frame和tk.Tk).
| 归档时间: |
|
| 查看次数: |
889 次 |
| 最近记录: |