如何在另一个类的实例上使用tkinter grid()?

时间:2019-03-27 10:55:17

标签: python-3.x class oop tkinter

我目前正在尝试使用Python编写pdf合并代码,并且正在使用tkinter来编写应用程序的GUI。请注意,这是我第一个使用Python的真实项目。

我已经为我的主应用程序编写了一个类,并创建了另一个名为BrowseButton的类,以便更轻松地操纵从tkinter.Button继承的特殊类型的按钮。

但是,使用grid()放置后,通过此类创建的按钮不会出现在主应用程序中。

这是我的代码

import tkinter as tk
from tkinter import filedialog
import openFile

class mainApplication(tk.Tk):
    def __init__(self, parent=None):
        tk.Tk.__init__(self, parent)
        self.parent=parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.browseLabel=tk.Label(self, text='Select the 2 PDF files to merge.', anchor='w', fg='black')
        self.browseLabel.grid(row=1, columnspan=10, pady=20)

        createQuitButton(self)
        createMergeButton(self)

        self.browseButton1=browseButton(self)
        self.browseButton2=browseButton(self)

        self.browseButton1.button.grid(row=5, column=2)
        self.browseButton2.button.grid(row=5, column=9)
        self.browseButton1.buttonLabel.grid(row=4, column=2)
        self.browseButton2.buttonLabel.grid(row=4, column=9)

    def merge(self):
        pass

class browseButton(tk.Button):
    def __init__(self, parent=None):
        tk.Button.__init__(self, parent)
        self.parent=parent
        self.createButton()

    def openFile(self):
        openFile.askOpenFile()
        self.changeButtonLabel()

    def createButton(self):
        browseButton.buttonLabel=tk.Label(self, text='Select file', anchor='w', fg='black')
        browseButton.button=tk.Button(self, text='Browse', command=self.openFile)

    def changeButtonLabel(self):
        self.buttonLabel['text']='File Found!

请有人告诉我为什么这不起作用吗?

0 个答案:

没有答案