Home>
I want to solve

A program that runs Tkinter's filedialog and selects a file while running another program. The first time it succeeds, but the second time the following error occurs. I want to prevent it.

code
import pyxel as px
import tkinter as tk
import tkinter.filedialog
import os
import threading
class App:
    def __init __ (self):
        self.sp = None # File path of the selected file
        px.init (250,125, caption = "")
        px.run (self.update, self.draw)
    def update (self):
        if px.btnp (px.KEY_Q):
            px.quit ()
        if px.btnp (px.KEY_F):
            threading.Thread (target = self.open_file) .start () # pyxel and tkinter run at the same time
    def draw (self):
        px.cls (0)
        px.text (0,0, str (self.sp), 7) #Display specified file path
    def open_file (self):
        fTyp = [("", "")]
        iDir = os.path.abspath (os.path.dirname (__ file__))
        selected_file_path = tkinter.filedialog.askopenfilename (filetypes = fTyp, initialdir = iDir) #Enter file path
        self.sp = selected_file_path
App ()
error
Exception in thread Thread-2:
Traceback (most recent call last):
  File "C: \ Users \ Sakuramochi \ AppData \ Local \ Programs \ Python \ Python39 \ lib \ threading.py", line 950, in _bootstrap_inner
    self.run ()
  File "C: \ Users \ Sakuramochi \ AppData \ Local \ Programs \ Python \ Python39 \ lib \ threading.py", line 888, in run
    self._target (* self._args, ** self._kwargs)
  File "c: \ Users \ Sakuramochi \ kpp \ Pyxel \ pyxel coder \ pyxel_coder.py", line 27, in open_file
    selected_file_path = tkinter.filedialog.askopenfilename (filetypes = fTyp, initialdir = iDir)
  File "C: \ Users \ Sakuramochi \ AppData \ Local \ Programs \ Python \ Python39 \ lib \ tkinter \ filedialog.py", line 382, ​​in askopenfilename
    return Open (** options) .show ()
  File "C: \ Users \ Sakuramochi \ AppData \ Local \ Programs \ Python \ Python39 \ lib \ tkinter \ commondialog.py", line 42, in show
    w = Frame (self.master)
  File "C: \ Users \ Sakuramochi \ AppData \ Local \ Programs \ Python \ Python39 \ lib \ tkinter \ __init__.py", line 3121, in __init__
    Widget.__init__ (self, master,'frame', cnf, {}, extra)
  File "C: \ Users \ Sakuramochi \ AppData \ Local \ Programs \ Python \ Python39 \ lib \ tkinter \ __init__.py", line 2569, in __init__
    self.tk.call (
RuntimeError: main thread is not in main loop
version

Python-Latest
tkinter-8.6.9
pyxel-1.4.3

  • Answer # 1

    As the error message says, there is no main loop.
    Let's create a main loop according to tkinter's method.

    It also states that pyxel and tkinter are running concurrently, which can be quite difficult.
    If you are a beginner, you should think about using the parts you use separately.

    For example, if you want to read a game scenario from a file, use load (tkinter) before the game starts, and then use pyxel.

  • Answer # 2

    I referred to the answer from Best Answer.

    def open_file (self):
        root = tk.Tk ()
        root.withdraw ()
        fTyp = [("", "")]
        iDir = os.path.abspath (os.path.dirname (__ file__))
        selected_file_path = tkinter.filedialog.askopenfilename (filetypes = fTyp, initialdir = iDir)
        self.sp = selected_file_path
        root.deiconify ()
        root.destroy ()


    Now you can hide unnecessary Tkinter windows and run them without errors.