Python Tkinter-如何将小类从一个类放到另一个类的窗口中

时间:2018-11-24 22:41:15

标签: python tkinter

我有一个来自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的一部分。

1 个答案:

答案 0 :(得分:1)

简而言之,您的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()