'OOP and Tkinter - how to create widgets
I'm trying to create my own Frame in tkinter but I have some problems with OOP. Below is my code.
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import *
class Listbox(tk.Listbox):
def __init__(self, master, addValues = False, values = None, *args, **kwargs):
tk.Listbox.__init__(self, master, *args, **kwargs)
# Tooltip
self.lListOver = [-1, None, None]
self.bind('<Motion>', lambda e: self.f_coord(e, l = self))
self.bind('<Leave>', self.f_resetnListOver)
if addValues:
for value in values:
self.insert(END, value)
def f_coord(self, e, l):
xEnrPos, yEnrPos = e.x, e.y
idxOver = l.nearest(yEnrPos)
lListCom = l.get(0, END)
tLast = l.bbox(len(lListCom) - 1)
if tLast is not None:
yDownLast = tLast[1] + tLast[3]
if yEnrPos > yDownLast:
if self.lListOver[1] is not None:
self.f_resetnListOver(None)
return None
if idxOver != self.lListOver[0]:
sX, sY = str(l.winfo_pointerx() + 15), str(l.winfo_pointery() + 15)
if self.lListOver[1] is not None: self.lListOver[1].destroy()
self.lListOver[1] = tk.Toplevel(l.master)
self.lListOver[1].wm_geometry("+" + sX + "+" + sY)
self.lListOver[1].wm_overrideredirect(True)
tk.Label(self.lListOver[1], text = lListCom[idxOver], bg = "#217346", bd = 1, justify = tk.LEFT).pack(padx = 2, pady = 2)
self.lListOver[0] = idxOver
return None
def f_resetnListOver(self, *d):
if self.lListOver[1] is None: return None
self.lListOver[0] = -1
self.lListOver[1].destroy()
self.lListOver[1] = None
self.lListOver[2] = None
return None
class ListboxFrame(ttk.Frame):
def __init__(self, master, labelText, *args, **kwargs):
ttk.Frame.__init__(self, master)
# Listbox
ttk.Label(self, text = labelText).grid(row = 0, column = 0, columnspan = 3)
self.listbox = Listbox(self, height = 5, *args, **kwargs)
self.listbox.grid(row = 1, column = 0, columnspan = 3, padx = 2, pady = 2)
# Entry
self.entry = ttk.Entry(self)
self.entry.grid(row = 2, column = 0, padx = 2, pady = 2)
# Buttons
self.addButton = ttk.Button(self, text = "Add", width = 4, command = self.Add)
self.addButton.grid(row = 2, column = 1, padx = 1, pady = 1)
self.deleteButton = ttk.Button(self, text = "Delete", width = 6, command = self.Delete)
self.deleteButton.grid(row = 2, column = 2, padx = 1, pady = 1)
def Add(self):
if self.entry.get() == "": return
self.listbox.insert(END, self.entry.get())
self.entry.delete(0, END)
def Delete(self):
for index in reversed(self.listbox.curselection()):
self.listbox.delete(index)
# listbox.config(height = listbox.size())
root = tk.Tk()
frame = ListboxFrame(root, "Listbox", addValues = True, values = ["Name", "Surname"], height = 10)
frame.pack()
root.mainloop()
What I'm trying to do here is to set the default height of my listbox in Frame to 5
self.listbox = Listbox(self, height = 5, *args, **kwargs)
but then when I want to create an instance of this Frame, I would like to pass another value of height
frame = ListboxFrame(root, "Listbox", addValues = True, values = ["Name", "Surname"], height = 10)
Here I'm also passing addValues and values arguments of my Listbox class. Unfortunately my code doesn't work and I got the error:
33
34 root = tk.Tk()
---> 35 frame = ListboxFrame(root, "Listbox", addValues = True, values = ["Name", "Surname"], height = 10)
36 frame.pack()
37 root.mainloop()
<ipython-input-135-115c7d7ebda4> in __init__(self, master, labelText, *args, **kwargs)
5 # Listbox
6 ttk.Label(self, text = labelText).grid(row = 0, column = 0, columnspan = 3)
----> 7 self.listbox = Listbox(self, height = 5, *args, **kwargs)
8 self.listbox.grid(row = 1, column = 0, columnspan = 3, padx = 2, pady = 2)
9
TypeError: type object got multiple values for keyword argument 'height'
I'm pretty new in OOP and Tkinter hence I hope you will be able to help me with this code to make it work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
