0
votes

I am using Ubuntu 18.04 with python 3.8.

This is the problem I have been experiencing since I have started using Tkinter.

Whenever I write the code, for some reason, the GUI gets stuck while loading. I speculate that this is because the program I intend to write involves lots of buttons (specifically 15x15=225). However, instead of loading slower or what not, the GUI window straight up refuses to show up.

The only 'pseudo-solution' which I found coincidentally, is pressing the ctrl key.

Why is this happening and why does pressing the ctrl key solve this problem?

Here is the code: To run, install tkinter and type python3 controller.py

model.py

class Model:
    def __init__(self):
        '''
        '''

controller.py

 from model import Model
 from view import View
 
 class Controller:
     def __init__(self):
         self.model = Model()
         self.view = View(self)
     def main(self):
         self.view.main()
 if __name__ == '__main__':
     scrabble = Controller()
     scrabble.main()

view.py

 import tkinter as tk
 from tkinter import ttk
 
 class View(tk.Tk):
     PAD = 15
     def __init__(self,controller):
         super().__init__()
         self.controller = controller
         self.value_var = tk.StringVar()
         self.title('Scrabble')
         self._make_main_frame()
         self._make_buttons()

     def main(self):
         self.mainloop()
 
     ##Private Methods
     def _make_main_frame(self):
         self.main_frm = ttk.Frame(self)
         self.main_frm.pack(padx=self.PAD, pady=self.PAD)
 
     def _make_buttons(self):
         outer_frm= ttk.Frame(self.main_frm)
         outer_frm.pack()
 
         frm = ttk.Frame(outer_frm)
         frm.pack()
         btns_row = 0 
         for i in range(self.PAD*15):
             if btns_row == self.PAD:
                 frm = ttk.Frame(outer_frm)
                 frm.pack()
                 btns_row = 0
             btn = ttk.Button(frm, text=i)
             btn.pack(side='left')
             btns_row += 1

2
Are you pressing <kbd>CTRL</kbd> + <kbd>C</kbd> by any chance? - 10 Rep
@10Rep Nope. I just tried it now and it just kills the program. It's like the program is on infinite loop until I press CTRL key for some reason. - Johnny Park
If you do press CTRL-C, what's the full traceback message you get? That might tell you where it was hung up. - jasonharper
Traceback (most recent call last): File "controller.py", line 12, in <module> scrabble.main() File "controller.py", line 9, in main self.view.main() File "view.py", line 15, in main self.mainloop() File "/usr/lib/python3.6/tkinter/__init__.py", line 1283, in mainloop self.tk.mainloop(n) - Johnny Park
Which seems fine as it also prints this exact message if the GUI is successfully loaded. - Johnny Park

2 Answers

0
votes

Interestingly, when I have upgraded my ubuntu from 18.04 to 20.04, this problem has seem to be gone. Out of curiosity, I have increased the amount of buttons to be loaded. After doing so, the problematic behavior has came back.

Recently I have got a chance to test if this problem exists in other computers. It seems that other computers (such as Acer) straight up doesn't load and seem to be stuck forever (or have not found a method yet to escape from being stuck).

Hence, I think the problem is in the tkinter itself (or perhaps it was not supposed to be used this way). As of now, there is nothing to be done other than to avoid using 200+ buttons (at least in ubuntu tkinter).

0
votes

I've run into the same problem running Tkinkter on ubuntu on arm64. I tried loading other applications found on github, and all seem to behave the same. To me, it seems more like a priority issue or race condition, and the more buttons the more chance for the condition to occur.

I've found a "work around", which is less than ideal, but allows me to use my application. NOT what I would call a solution.

By lowering the "niceness" of the application, which increases the priority of the process, everything loads fast and completely. The downside to this is that you need to run your application with administrative privileges. And there might be other concerns/side effects with changing the priority as well.

import os

# decrease the niceness by 1 to increase the process priority
os.nice(-1)