class Application(tk.Frame):中调用其他class里的内容应该如何编写代码
时间: 2024-02-22 15:00:41 浏览: 14
如果您想在`Application`类中调用其他类的内容,可以在`Application`类的构造函数中创建其他类的实例,并将这些实例作为`Application`类的属性。例如:
```python
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.other_class_instance = OtherClass()
def some_method(self):
self.other_class_instance.some_method()
```
在上面的代码中,`Application`类创建了`OtherClass`类的实例,并将其存储为属性`other_class_instance`。然后,`Application`类中的`some_method()`方法可以使用`other_class_instance`来调用`OtherClass`类的某个方法。
相关问题
class Application(tk.Frame):怎么结束这个函数
在 Python 中,类定义的是一个对象,它本身也不是一个函数,因此也不存在“结束 class 函数”这样的说法。但是,如果你在类中定义了方法(method),那么可以通过在方法中使用 return 语句来结束方法的执行。例如:
```python
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red",
command=self.master.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
return # 结束方法
```
在上面的代码中,Application 是一个继承了 tk.Frame 的类,它有一个 say_hi() 方法,当用户点击按钮时,该方法会被调用,打印一条消息并使用 return 语句结束方法的执行。
文件salary.py代码如下:import tkinter as tk from tkinter import messagebox from tkinter.ttk import Treeview class Application(tk.Frame): def __init__(self, master): super().__init__(master) self.place(relx=0, rely=0, relwidth=1, relheight=1) self.master = master
这段代码是一个基于tkinter库的GUI应用程序的基础框架,它创建了一个名为Application的类,并继承了tk.Frame类。在构造函数中,它使用super()函数调用父类的构造函数,并将主窗口(master)作为参数传递给父类构造函数。它还使用place()方法将整个窗口放置在主窗口的左上角,并设置其相对大小为1。
但是,这段代码似乎并没有完成,因为它没有定义任何的窗口组件。如果您想使用这个框架来创建一个实际的GUI应用程序,您需要添加一些窗口组件,例如按钮、文本框、标签、菜单等。