Python Forum
Code fails on Mac, works on Windows and Raspberry Pi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code fails on Mac, works on Windows and Raspberry Pi
#1
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()
Reply
#2
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()) 
Reply
#3
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:

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

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Listbox search code partially works chesschaser 9 3,766 May-05-2020, 01:08 PM
Last Post: chesschaser
  [Tkinter] Tkinter GUI works on Windows but not on Ubuntu danbei 8 9,032 Jun-27-2017, 08:03 PM
Last Post: danbei
  popup message box code runs in Windows, but not in Linux Luke_Drillbrain 13 14,786 May-10-2017, 01:05 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

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