Noura Ibrahim
Noura Ibrahim

Reputation: 1

Why does the Tkinter OptionMenu crash my code (most of the time) when clicked on a second window?

Tkinter inconsistently crashes when using OptionMenu on a second window V. 8.6.11 and 8.6.12

I am trying to create 2 independent tkinter windows where the second one only shows up after the first is closed. I want the second window to have an OptionMenu (as well as some other things). I keep running into an inconsistent issue of my program crashing when I click on the OptionMenu. By inconsistent I mean that I could run it 3 times in a row and it works but then it fails on the 4th. Most of the time it does fail. The errors I get are also inconsistent. I have seen three errors in the terminal and I haven't been able to see a pattern for when each shows up. It's seemingly random because I'm running the same code over and over again. Here are the error messages:

  1. zsh: segmentation fault python myProgram.py (I get this the majority of the time)
  2. zsh: bus error python myProgram.py
  3. zsh: trace trap python myProgram.py

I've only encountered this issue with the OptionMenu widget. I could have buttons, checkboxes, entry boxes, or even a ComboBox and everything works just fine so I am at a loss.

This issue happens with tkinter versions 8.6.11 and 8.6.12 but downgrading to 8.6.10 seems to fix it. So I am not sure if this is a bug with the newer tk versions or if I am doing something incorrectly. I know that people usually don't recommend creating multiple instances of Tk() but I need the windows to be independent and I believe I am terminating the first instance before making the second so they shouldn't interfere (Please correct me if I am wrong as I am still new to Tkinter).

I tried 2 different simple versions of making this program and they both have the same crashing issue. I prefer the first version so that I can contain every window in its own function and make them independent of each other. Eventually, I would like to import these functions into another script and use as needed which is why I would like to avoid using a Toplevel. Again these work just fine with tkinter 8.6.10 but crash with 8.6.11 and 8.6.12.

Out of desperation, I tried different combinations of Python and Tkinter versions as well. This code works with:

With Python 3.9.11+ (with the exception of 3.10.0), I can't conda install the downgraded version of Tkinter and I have to go with 8.6.11 or 8.6.12 due to incompatibility. I would prefer to use the latest (stable) versions so I would appreciate any help debugging this issue. For reference, I am using MacOS Ventura 13.3.1

Version 1

from tkinter import *

def window1():
    first_win = Tk()
    first_win.geometry("420x350") 
    exit_button = Button(first_win, text="Kill and open new window", command=first_win.destroy).pack()
    first_win.mainloop()

def window2():
    second_win = Tk()
    second_win.geometry("420x350")   
   
    var = StringVar()
    options= ('a', 'b','c')
    var.set('a')
    OptionMenu( second_win , var, *options).pack()  

    Button(second_win,text = 'kill',command = second_win.destroy).pack()
    second_win.mainloop()

window1()
window2()   

Version 2

This way is a bit more cluncky as it links the two functions. I tried it based on an example from this exchange

from tkinter import *



def new_win():
    first_win.destroy()
    second_win = Tk()
    second_win.geometry("420x350")   
   
    var = StringVar()
    options= ('a', 'b','c')
    var.set('a')
    OptionMenu( second_win , var, *options).pack()    

    Button(second_win,text = 'kill',command = second_win.destroy).pack()
    second_win.mainloop()

first_win = Tk()
first_win.geometry("420x350") 
exit_button = Button(first_win, text="Kill and open new window", command=new_win).pack()
first_win.mainloop()

Any help would be greatly appreciated. Let me know if you need more information

Upvotes: 0

Views: 112

Answers (1)

relent95
relent95

Reputation: 4742

It's a bug in Tcl/Tk. See the issue ef5d3e29a4 for details. For now, avoid to create multiple tkinter root interpreters.

Upvotes: 0

Related Questions