i want a button that uses my selectpath function but i cant figure out how to get it working inside a class. any help ist much appreciated!
i always get this error when i execute it.
TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
import xlsxwriter
import os
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showerror
from tkinter.filedialog import askdirectory
class SSLCreator(ttk.Frame, tk.filedialog):
def __init__(self, container):
super().__init__(container)
# field options
options = {'padx': 5, 'pady': 5}
# selectPath label
self.selectPath_label = ttk.Label(self, text='Ordner')
self.selectPath_label.grid(column=0, row=0, sticky=tk.W, **options)
self.Path = tk.StringVar()
# selectPath entry
self.selectPath_entry = ttk.Entry(self, textvariable=self.selectPath)
self.selectPath_entry.grid(column=1, row=0, **options)
self.selectPath_entry.focus()
# selectPath button
self.selectPath_button = ttk.Button(self, text='Öffnen')
self.selectPath_button['command'] = self.selectPath
self.selectPath_button.grid(column=2, row=0, sticky=tk.W, **options)
# seperator label
self.seperator_label = ttk.Label(self, text='Seperator')
self.seperator_label.grid(column=0, row=1, sticky=tk.W, **options)
self.seperator = tk.StringVar()
# seperator entry
self.seperator_entry = ttk.Entry(self, textvariable=self.seperator)
self.seperator_entry.grid(column=1, row=1, **options)
self.seperator_entry.focus()
#self.convert_button = ttk.Button(self, text='Convert')
#self.convert_button['command'] = self.convert
#self.convert_button.grid(column=2, row=2, sticky=tk.W, **options)
# result label
self.result_label = ttk.Label(self)
self.result_label.grid(row=2, columnspan=3, **options)
# add padding to the frame and show it
self.grid(padx=10, pady=10, sticky=tk.NSEW)
def selectPath(self):
path_ = self.askdirectory()
self.Path.set(path_)
class App(tk.Tk):
def __init__(self):
super().__init__()
self.iconbitmap("C:\\Users\\Michael\\Desktop\\icon.ico")
self.title('SSL Creator')
self.geometry('300x110')
self.resizable(False, False)
if __name__ == "__main__":
app = App()
SSLCreator(app)
app.mainloop()
CodePudding user response:
The root of the problem is that you're inheriting from both ttk.Frame
and tk.filedialog
. This simply can't work, you can't inherit from both. Your new class can't both be a frame and a dialog at the same time.
If you want your SSLCreator
class to have access to a file dialog or its methods, you just need to call the method inside your class.
class SSLCreator(ttk.Frame):
def __init__(self, container):
super().__init__(container)
...
def selectPath(self):
path_ = askdirectory()
self.Path.set(path_)