TKinter, pygubu - opening filedialog.askdirectory() hangs program - python

I'm building an application using tkinter (and pygubu, this is relevant). In my application I need to browse a directory. This is the code of a button click callback (it's just inserting the path of the directory in a textbox object.
def chose_folder(self):
folder = fd.askdirectory()
txt = self.builder.get_object('folder_txt')
txt.delete(0, tk.END)
txt.insert(0, folder)
I've tried this code on Linux Mint 20.2, and it works fine, but on Windows 10, when I click the button, the program hangs and need to be forcibly terminated. However, if I change fd.askdirectory() with fd.askopenfilename(), or literally any other type of dialog, it works fine. The main difference I can see is that on linux, tkinter uses a custom dialog, while on Windows is uses the native Windows Explorer dialog.
Weirdly enough, I tried this minimal code
from tkinter import *
from tkinter.messagebox import *
from tkinter import filedialog as fd
file_name = fd.askdirectory()
print (file_name)
and it works perfectly, so I'm guessing the problem must be that I'm using pygubu to design the ui, but I can't figure out where the problem precisely is. I've found some old issues about this.

Related

Displaying other files of the same directory in a file in Python

I am creating a piece of software for my organisation which I recently started, and I am using the Tkinter module in Python to create my software. I have created a word processor and an online encyclopaedia for the software.
The idea is that the main program is where you can access all the applications just by the click of a button - similar to how a desktop functions. I have created the main function that will open the applications, but when you run the program, it opens the application first and then the main program where you access the application. Everything is working fine, and there are no error messages which show up when I run the program.
I've tried researching any solutions to my problem, but they don't give me anything that I can do to fix my problem. How I have structured my function for opening the applications is that I have created a function for each application and in the Button() function, that function for the application will be the command.
Here is the code for the software that I am developing:
from tkinter import *
import time
import os
import Wordee as wordee
window = Tk()
window.title("Brainwave One")
window.iconbitmap('Brainwave-One.ico')
def get_datetime():
timeVariable = time.strftime("%I:%M %p, %A %d %B %Y")
date_time.config(text=timeVariable)
date_time.after(200,get_datetime)
def wordee():
print(wordee)
wordee = Button(window,text="Wordee",font=("Calibri",23),command=wordee)
wordee.place(relx=0.25,rely=0.1875,anchor="center")
date_time = Label(window,font=("Calibri",15))
date_time.grid(row=0,column=0)
title = Label(window,text="Brainwave One",font=("Calibri",40))
title.place(relx=0.5,rely=0.05,anchor="center")
title = Label(window,text="From The Brainwave Incorporation",font=("Calibri",13))
title.place(relx=0.5,rely=0.083,anchor="center")
get_datetime()
window.mainloop()

Python tkinter askopenfilename was working for me and is now not responding

I'm developing a program with Python and tkinter that begins with the user adding a text file from their directory. I had built a GUI with tkinter that provided a button to be pressed and a popup window for them to select their file--it was working fine, and then suddenly when I tried to run it it would begin "Not Responding" when I pressed the button meant to launch the popup window.
I am running Python 3.7.3 on Windows 10 in Jupyter notebooks; the tkinter version is 8.6. I have 8 GB of RAM but I'm not using more than 80% of it.
I've tried looking at some similar Stack Overflow questions like the ones here and here:
windows thinks tkinter is not responding
Python tkinter askopenfilename not responding
Trying askopenfilenames() didn't work; neither did adding root.update() or %gui tk.
Here is the code I'm working with:
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import *
root = Tk()
topFrame=Frame(root)
topFrame.pack()
middleFrame=Frame(root, width=200, height=250,
highlightbackground="yellow",
highlightthickness=3,
borderwidth=2,
relief=RAISED)
middleFrame.pack()
bottomFrame=Frame(root)
bottomFrame.pack()
ourdirectory=[]
def load1():
f1 = askopenfilename(filetypes=(('TXT files','*.txt'), ('All files', '*.*')))
ourdirectory.append(f1)
mylab = Label(topFrame, text="Hello and welcome!")
mylab.pack()
button = Button(bottomFrame, text="Add File", command=load1)
button.grid(row=5, sticky=W)
root.mainloop()
When the "Add File" button is pressed, I expected the program to launch a popup window where the user can navigate in the file directory. Instead, the title text just fades and it doesn't respond--and clicking on the window causes it to announce that it's stopped responding. (This also consistently causes the Jupyter kernel to die.) Still, when I run the code but close the main window without pressing the "File Add" button, it closes fine and nothing freezes or breaks.
I have encountered the same issue when running some code that would open, modify, and overwrite an image. Everything worked as expected until suddenly askopenfilename() started Not Responding and not opening the file dialog window.
I have attempted changing the initial directory of askopenfilename() and calling the method from a separate file and the command line without success.
One of my open windows was a directory (not the same as I was working with) on which file thumbnails failed to load and files could not be opened. Upon closing the window, the screen 'reloaded,' certain programs opened (such as sticky notes, which was previously closed but which typically opens upon restarting), and the method resumed normal functionality.

I have an issue with running another Python file with tkinter

I am trying to link my code that runs motors from tkinter condition. I didn't get any methods to do so.
I have tried hyperlink method, but it only opens the file on my browser.
...python
from tkinter import *
import webbrowser
def callback(url):
webbrowser.open_new(url)
root = Tk()
link1 = Label(root, text="Running Hyperlink", fg="blue", cursor="hand2")
link1.pack()
link1.bind("<Button-1>", lambda e: callback(r"/home/pi/ROBOT/pdf folder/GUIfunal1.py"))
root.mainloop()
The statement webbrowser.open_new(url) opens the python script on the browser, but does not run it.
Without setting up a Flask server and loading the python script to the server, a python script will not be run. (There might be other ways, but Flask server is the one I know of).
If you do not want to hyperlink it, running it like exec('path to python file.py') should run it.

Problem with tkinter code execution order

I have my principal script running with terminal that works perfectly. Im trying to make a gui for it but im stuck at this point.
Like you see on the screen, at the start of the script it asks if it should check the database. And just after, it asks first the platform before opening the captcha for the database check. The problem happens exactly here on my GUI version, look.
Like you see, the gui starts, but when i click on check for new database, it directly opens the captcha without asking the platform... And it asks me the platform only after i solved the captcha which i dont want to after...
Here is the main testkinter.py code:
import tkinter as tk
from tkinter import messagebox
import commands
import CheckDatabase
import SetPlatformfile
def check_and_hide():
CheckDatabase.db_download(root)
checkdb.pack_forget()
checkdb1.pack_forget()
root = tk.Tk()
checkdb = tk.Button(root, text="Check for new databases", command=check_and_hide)
checkdb.pack()
checkdb1 = tk.Button(root, text="No")
checkdb1.pack()
root.mainloop()
Here is the set_platform function called in the Checkdatabse file:
import tkinter as tk
import config
from tkinter import messagebox
def set_platform(root):
platform = tk.Label(root,text="'a'|Android -- 'i'|iOS: ")
platform.pack()
androidbutton=tk.Button(root,text="Android",command=renameplatformandroid)
iosbutton=tk.Button(root,text="iOS",command=renameplatformios)
androidbutton.pack()
iosbutton.pack()
def renameplatformandroid():
config.platform = 'android'
print(config.platform)
def renameplatformios():
config.platform = 'ios'
print(config.platform)
And cuz of my checkdatabase file is really really long, i'll just put a screen at the exact moment where set_platform is called (its called in the func signup which itself is directly called at the beginning of db_download) .
I hope my question is clear! Let me know if you need more details.

Bash script to run Python Tkinter GUI

I am trying to write a Bash script so that I can run my program on a double click. The program uses tkinter and the GUI is the only thing I need to see. My bat file is the following:
python BudgetGUI.py &
This runs the code and successfully prints any print statements I have throughout my code but it never opens up the GUI. It simply runs through and closes immediately.
How can I modify the bash script to run the GUI?
Thanks in advance!
Edit Solutions for both mac and pc would be great, though at the moment I am on PC. I am working in Python3.
You need to add a call to mainloop(). I can't say for sure without seeing your code, but probably you need to add root.mainloop() to the bottom.
You don't need a bash or bat file. For your mac, just add a shebang and make the file executable. For Windows, add a shebang and associate the file with py.exe.
If you want to suppress the command line from popping up along with the GUI, rename your file with a .pyw extension.
Make the window made with tkinter stay out of IDLE*
if you have
root = Tk()
at the end put
root.mainloop()
if you have another name for that like:
window1 = Tk()
then, at the end put
window1.mainloop()
and so for any name you gave to the istance of Tk()
A little example
import tkinter as tk
class Window:
root = tk.Tk()
label = tk.Label(root, text = "Ciao").pack()
app = Window()
app.root.mainloop()
Python 2
The code above is for python 3.
For python 2 you just need to change the first line (Tkinter with T, not t)
import Tkinter as tk

Categories