Jan-30-2026, 04:26 PM
I am attaching two .py and want to write a third .py in which I can have 1's functionality (up/down navigation) in 2nd' look and feel (Toplevel).
|
Merge Script Functionality in a Single Script
|
|
Jan-30-2026, 04:26 PM
I am attaching two .py and want to write a third .py in which I can have 1's functionality (up/down navigation) in 2nd' look and feel (Toplevel).
Jan-30-2026, 05:35 PM
Open both files with a tool such as Kdiff3 or Meld or other merge tools and select the parts that you want of each script in a third one.
« We can solve any problem by introducing an extra level of indirection »
Jan-31-2026, 01:18 AM
(Jan-30-2026, 05:35 PM)Gribouillis Wrote: Open both files with a tool such as Kdiff3 or Meld or other merge tools and select the parts that you want of each script in a third one. Thanks for your reply. The problem is coding and my lower level of knowledge. It's not just picking the parts of scripts. There is definitely some more stuff required to have up/down keys navigation as ex1 and look and feel of ex2. I tried my best but failed so I kept in two scripts, please help me.
Feb-03-2026, 05:51 AM
What exactly do you want to achieve? Your examples look complicated, a bit hard to follow.
Here is a simple window and combobox. What functionality would you like to add? def win():
root = tk.Tk()
root.geometry("400x400")
root.title("VB Navigation Demo")
def show():
if cb.get() == "Hedgehog":
label3.config(text="Ouch, bit spiky this one " + cb.get())
else:
label3.config(text="You chose: " + cb.get())
# dropdown options
a = ["Apple", "Banana", "Cherry", "Date", "Elderberry", "Hedgehog"]
# label to show selected value
label1 = tk.Label(root, text="Have a look in my lovely combobox below! ")
label1.pack(pady=10)
label2 = tk.Label(root, text="Choose a delicious fruit! ")
label2.pack(pady=10)
# Combobox starts here
cb = tk.ttk.Combobox(root, values=a)
cb.set("Select a fruit")
cb.pack(pady=10)
# button to display selection
tk.Button(root, text="Show Selection", command=show).pack(pady=10)
# label to show selected value
label3 = tk.Label(root, text="Click Show Selection to see your choice: ")
label3.pack()
root.mainloop()I start win() in Idle and see a nice little window. What else do you want to add? Keyboard navigation?
Feb-03-2026, 10:05 PM
What is wrong with this?
import tkinter as tk
from tkinter import ttk
class VBComboBox(ttk.Combobox):
"""A Combobox."""
def __init__(self, master, values, width=25, max_visible=6):
super().__init__(master, values=values, width=width, height=max_visible)
class DemoForm(tk.Tk):
def __init__(self):
super().__init__()
self.title("VB6 ComboBox Behavior Demo")
self.geometry("420x300")
self.txt1 = tk.Entry(self, width=30)
self.txt1.pack(pady=(30, 10))
values = [
"Apple",
"Apricot",
"Banana",
"Blackberry",
"Blueberry",
"Cherry",
"Date",
"Grape",
"Mango",
"Orange",
"Peach",
"Strawberry",
]
self.combo = VBComboBox(self, values)
self.combo.pack()
self.txt2 = tk.Entry(self, width=30)
self.txt2.pack(pady=10)
DemoForm().mainloop()Tabbing moves through the widgets in the window and up/down arrows move through the combobox items, just like every other tkinter application. Your examples don't work as I expect, and because of that, they are unpleasant (to me) to use. It is ambiguous what will happen when an arrow key or tab is pressed. ex2 is particularly jarring, not working or looking as expected.Why do you want a different combobox? Know that changing things like how tabbing and arrow keys work on widgets is going to be a lot of work
Feb-03-2026, 11:31 PM
@deanhystad I don't know much about classes. Can you explain this a little?
As I understand it, we make a class, then create an instance of that class. Here, you just call the class directly: DemoForm().mainloop()There is no : win = DemoForm() win.mainloop()In fact, if I write win =DemoForm()The window opens directly! Is that supposed to happen?
Feb-04-2026, 05:01 PM
(This post was last modified: Feb-04-2026, 05:01 PM by deanhystad.)
This
win =DemoForm()Only opens a window because you are running in IDLE. If you run the program from the command prompt the window would only open for an instant and disappear (or not appear at all). The window doesn't disappear when you run the program in IDLE because you aren't really running the program. You are running IDLE and IDLE is interpreting the program. When the program is done, IDLE continues to run, preventing the automatic cleanup that happens when a program ends. Run the program directly from the command prompt and you'll see the difference. There's no reason for win in this: win = DemoForm() win.mainloop()It's kind of like doing this: win = 2 + 3 y = win ** 4instead of this: y = (2 + 3 )** 4There may be times when you want to keep a reference to a toplevel window that was closed, but that would be unusual.
Feb-04-2026, 11:09 PM
(This post was last modified: Feb-05-2026, 02:01 AM by Pedroski55.)
@deanhystad Thanks very much for your advice.
One more question if I may: I read, super.__init__() is used to initialize a parent window class from a child window class. You have super.__init__() in both classes. What's going on there? Can confirm, the programme runs fine in bash, after adding the famous if __name__ == "__main__": line, of course!Quote:peterr@peterr-Modern-15-B7M:~/PVE$ python3 tkinter/combobox_select_item_classes.py
Feb-04-2026, 11:47 PM
super.__init__() is used to call the __init__() method(s) of the superclass(es). The plural is there because a class may inherit from multiple classes.
This is usually done because the superclass(es) contain initialization that is required to make the class work. In the example, DemoForm() does not contain any code that initializes a top level window. Nor does it call code that initializes the tkinter framework. This code has to execute, or DemoForm will fail to work. The missing code is called by running Tk.__init__(). For VBComboBox the __init__() method is only there to change the variable signature to map the VBComboBox in the original example. I didn't want to change much from the original post, and I wanted it to be obvious that the ttk combobox does a good job handling arrow key and tab presses. So this is kind of correct in spirit while still being completely wrong: Quote:I read, super.__init__() is used to initialize a parent window from a child window. __init__() is a superclass inheritance thing, not a parent/child thing. Parent/child is not the terminology used when discussing inheritance. Those words are used to define different relationships. The demo window object and the combo widget inside the demo window have a parent/child relationship. The widget is a child of the window. The widget and the window are different classes and the man not have any shared behaviors. DemoWindow calling __init__() does not initialize a parent window. Instances of DemoWindow are Tk windows with extra code. Calling super().__init__() from inside DemoWindow() __init__() is calling Tk.__init__() which initializes the newly created instance of DemoWindow. If DemoWindow did not call super().__init__() it would not be a correctly initialized window, and would probably cause the program to crash. It distinction may sound nitpicky, but it is an important distinction. |
|
|