Python Forum
Merge Script Functionality in a Single Script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Merge Script Functionality in a Single Script
#1
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).

Attached Files

.py   ex1.py (Size: 5.16 KB / Downloads: 9)
.py   ex2.py (Size: 7.31 KB / Downloads: 9)
Reply
#2
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 »
Reply
#3
(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.
Reply
#4
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?
Reply
#5
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
Pedroski55 likes this post
Reply
#6
@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?
Reply
#7
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 ** 4
instead of this:
y = (2  + 3 )** 4
There may be times when you want to keep a reference to a toplevel window that was closed, but that would be unusual.
Reply
#8
@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
Reply
#9
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.
Pedroski55 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need a script to replace an existing Python installation in a version-independent dir pstein 7 146 May-09-2026, 01:04 PM
Last Post: DeaD_EyE
  Py script to APP on MAC tester_V 3 775 Dec-05-2025, 08:12 PM
Last Post: tester_V
  Trying to install the edk2-rk3399 code but the script fails due some python bug mariozio 2 1,919 Aug-17-2025, 10:25 PM
Last Post: mariozio
  Running script from remote to server invisiblemind 4 1,837 Mar-28-2025, 07:57 AM
Last Post: buran
  Insert command line in script lif 4 2,200 Mar-24-2025, 10:30 PM
Last Post: lif
  modifying a script mackconsult 1 1,128 Mar-17-2025, 04:13 PM
Last Post: snippsat
  help with a script that adds docstrings and type hints to other scripts rickbunk 1 1,894 Feb-24-2025, 05:12 AM
Last Post: from1991
  Detect if another copy of a script is running from within the script gw1500se 4 2,111 Jan-31-2025, 11:30 PM
Last Post: Skaperen
  pass arguments from bat file to pyhon script from application absolut 2 2,880 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Best way to feed python script of a file absolut 6 2,504 Jan-11-2025, 07:03 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020