1

I have a large list of environments loaded into an OptionMenu. When you open it, it will immediately close once you let go of the mouse button if the opened menu reaches outside of the screen. Is there any way to deal with this? (for example making it smaller and scrollable or deactivating said function) I tested this on Linux20.04 in case it is different on windows. Or can someone mention a dropdown-menu that doesn't have that problem.

#!/usr/bin/env python3
from tkinter import *

class Dropdown:
    def __init__(self,options, master):
        self.variable = StringVar(master)
        self.variable.set(options[0]) # default value
        self.menue = OptionMenu(master, self.variable, *options)
        self.menue.pack()

typ = [
    "Giftpflanze",
    "Übernatürliche Pflanze",
    "Heilpflanze",
    "Nutzpflanze",
    "Wehrende Pflanze",
    "physische Wirkung",
    "psychische Wirkung"
]

# Rad Suchschwierigkeit
# Rad Bestimmschwierigkeit


master = Tk()

d1 = Dropdown(typ,master)


def ok():
    print ("value is:" + d1.variable.get())

button = Button(master, text="OK", command=ok)
button.pack()

mainloop()

So you can try it out on your own (just make sure it actually would reach out of your screen).

Here is a video: https://i.stack.imgur.com/aandv.jpg

enter image description here

Natan
  • 728
  • 1
  • 7
  • 23
  • I cannot reproduce the error, or I just misunderstood the question –  Jul 15 '21 at 13:30
  • Avoid using option menu and use either the ttk combobox or popup a treeview widget. A combobox with prefix handling would help the user get to the right items usefully. A treeview could allow you to apply categories to reduce the length of the list shown. – patthoyts Jul 15 '21 at 13:31
  • 1
    I could not reproduce this on Windows 10, Raspbian or Ubuntu 16 – Henry Jul 15 '21 at 16:15
  • 1
    irst of I strongly advise against using wildcard (`*`) when importing something, You should either import what You need, e.g. `from tkinter import Tk, Text, Entry` and so on or import the whole module: `import tkinter` then You can also use an alias: `import tkinter as tk` or sth like that, the point is that don't import everything unless You actually know what You are doing because today I came across an issue that was caused because of wildcard imports; name clashes are the issue. also I couldn't reproduce the issue, maybe You could show a gif or sth of that or try to explain it better – Matiiss Jul 15 '21 at 22:12
  • I added a video and my linux version so it becomes more clear what I meant. I will try what @patthoyts has suggested. The minimal example program presented above is not the actual program nor would I recommend using the wildcard in general. I am aware of it's implications and why you should refrain from using it in most cases. However: This is not one of them. I am new to tkinter not programming in general ;) thanks though and it might be helpful for other readers. – Natan Jul 16 '21 at 14:21
  • @patthoyts would you create an answer that I can accept? I will rephrase the question so that the answer is valid since it solved my problem in a different way then I expected. I created a new question that came up concerning comboboxes: https://stackoverflow.com/questions/68414759/combobox-enable-strga-to-select-text – Natan Jul 16 '21 at 20:12

0 Answers0