Python Forum

Full Version: Code fails on Mac, works on Windows and Raspberry Pi
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Everyone,

This is my first time posting in this forum - thanks for your patience. Here is a listing of a simple ScrollBar being attached to a Listbox with a Frame. This example works well on Windows 10, Raspberry Pi Stretch, and outputs many items on Mac High Sierra (10.13.4). The code is supposed to show a single message box per mouse click with the selected item number that was chosen by the user. On Mac it shows the number and then repeats a random amount of times (3-5 so far). Is there something specific that I need to do to make this work on Mac, or is this a possible bug?

Thanks :)

Eugene

Edit: All code is executed in IDLE 3.6.x

#Import the tkinter library
from tkinter import *
from tkinter import messagebox
from tkinter import Listbox
from tkinter import Scrollbar

#Function Definition
def CurSelet(evt):
    value=str((Listbox1.get(ANCHOR)))
    messagebox.showinfo("Selection","You selected: " + str(value))

#Create a window
window=Tk()
#Change the Window Title
window.title("Example2-5")
#Change the dimension of the window
window.geometry("300x200")

#Add a label
Label1=Label(window, text="View the listbox and click on a number",justify=CENTER)
Label1.pack(side="top")

#Add the Frame for alignment and then
#the Listbox and Scrollbar widgets
Frame1=Frame(window)
Frame1.pack(side="top")
Listbox1 = Listbox(Frame1)
Listbox1.pack(side="left",fill=Y)

ScrollBar1=Scrollbar(Frame1,orient=VERTICAL)
ScrollBar1.config(command=Listbox1.yview)
ScrollBar1.pack(side=RIGHT,fill=Y)
Listbox1.yscrollcommand=ScrollBar1.set

#Populate the listbox with data
for i in range(20):
    Listbox1.insert(END, str(i))

Listbox1.bind("<<ListboxSelect>>", CurSelet) #Listbox Selection

window.mainloop()
Add this line to the callback function and see if it prints the correct selection. Note that it is a tuple.
print("seleted =", Listbox1.curselection()) 
Hi woooee,
Thanks for the suggestion, and the values on Mac don’t seem random. When I run the program with your suggestion and click the #7 in the Listbox1 widget, here are the values I get from each Operating System:

Windows (see video)
selected=(7,)

Mac (see video)
selected=(7,)
selected=(7,)
selected=(8,)

Raspberry Pi Stretch
selected=(7,)

The interesting part is that the Listbox widget on Mac had the value 13 (unfocused) showing at the bottom of the Listbox1 widget before I clicked it. Listbox1 then continued to loop through the CurSelet(evt) function starting at 13 and continuing until the last value (19) was reached.
Here are links to a video with the same program running on Windows and running on a Mac.

Windows Video:


Here is the Mac video:

A work around may be to take the first value only on the Mac, but that is an odd error.
Thanks for your help wooee.

I'll use the workaround you suggested. Thanks again!