我有一个来自MainGUIApp类的窗口,还有一些来自另一个类的小部件,我希望将其放在此MainGUIApp类上。怎么做?
import tkinter as tk
from tkinter import ttk, Tk, Menu, Label, StringVar, OptionMenu, Entry, Button, messagebox, Canvas, HORIZONTAL, Text, END
class MainGUIApp(tk.Tk):
def __init__(self, window_title, window_width, window_length):
# Window settings
super(MainGUIApp, self).__init__()
self.title(window_title)
# get screen width and height
ws = self.winfo_screenwidth()
hs = self.winfo_screenheight()
# calculate position x, y
x = (ws / 2) - (window_width / 2)
y = (hs / 2) - (window_length / 2)
self.geometry('%dx%d+%d+%d' % (window_width, window_length, x, y))
self.current_directory = "C://Path"
self.current_company = "Test_Company"
self.top_panel = TopPanel.TopPanel(self, self.current_directory, self.current_company)
TopPanel类如下所示:
from tkinter import Tk
from tkinter import ttk, Tk
class TopPanel(tk.Tk):
def __init__(self, parent_frame, current_directory, current_company):
super(parent_frame, self).__init__()
self.current_directory = current_directory
self.show_current_directory()
def show_current_directory(self):
# Have a text for current directory, pad y by 20, and set anchor to w (west)
if self.current_directory is None:
current_directory_text = Label(self,
text="Current Directory:" + ' '
+ "No directory assigned",
font=("Helvetica", 12), anchor='w', pady=20)
else:
current_directory_text = Label(self,
text="Current Directory:" + ' '
+ self.current_directory,
font=("Helvetica", 12), anchor='w', pady=20)
current_directory_text.grid(row=0, sticky="w")
但是问题是:TopPanel作为新窗口打开。我希望TopPanel组件成为MainGuiApp的一部分。
最佳答案
简而言之,您的TopPanel
需要从tk.Frame
继承:class TopPanel(tk.Frame):
更改行,以便使用以下命令初始化tk.Frame
:super(TopPanel, self).__init__()
初始化TopPanel
实例时,请确保将其放置在以下位置:self.top_panel.grid(row=0, column=0, sticky="nsew")
import tkinter as tk
from tkinter import ttk, Tk, Menu, Label, StringVar, OptionMenu, Entry, Button, messagebox, Canvas, HORIZONTAL, Text, END
class MainGUIApp(tk.Tk):
def __init__(self, window_title, window_width, window_length):
# Window settings
super(MainGUIApp, self).__init__()
self.title(window_title)
# get screen width and height
ws = self.winfo_screenwidth()
hs = self.winfo_screenheight()
# calculate position x, y
x = (ws / 2) - (window_width / 2)
y = (hs / 2) - (window_length / 2)
self.geometry('%dx%d+%d+%d' % (window_width, window_length, x, y))
self.current_directory = "C://Path"
self.current_company = "Test_Company"
self.top_panel = TopPanel(self, self.current_directory, self.current_company)
self.top_panel.grid(row=0, column=0, sticky="nsew")
from tkinter import Tk
from tkinter import ttk, Tk
class TopPanel(tk.Frame):
def __init__(self, parent_frame, current_directory, current_company):
super(TopPanel, self).__init__()
self.current_directory = current_directory
self.show_current_directory()
def show_current_directory(self):
# Have a text for current directory, pad y by 20, and set anchor to w (west)
if self.current_directory is None:
current_directory_text = Label(self,
text="Current Directory:" + ' '
+ "No directory assigned",
font=("Helvetica", 12), anchor='w', pady=20)
else:
current_directory_text = Label(self,
text="Current Directory:" + ' '
+ self.current_directory,
font=("Helvetica", 12), anchor='w', pady=20)
current_directory_text.grid(row=0, sticky="w")
main = MainGUIApp("test", 500, 500)
main.mainloop()
关于python - Python Tkinter-如何将小类从一个类放到另一个类的窗口中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53462979/