I have an issue with running another Python file with tkinter - python

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.

Related

Having problems when starting a .R script using a button in a tkinter GUI

I'm currently working on a GUI for a R script I have written earlier. Since I have some experience in Python I decided to use tkinter for that.
The GUI should mainly consist of a few entry-options and a "start analysis" button. So far no problems.
I have written a function called call_r() which starts the R script using subprocess.run(...), the arguments will later come frome the users entries:
def call_r():
r_script = 'C:/Program Files/R/R-4.0.5/bin/Rscript.exe'
path_to_file = 'C:/.../Automated Analysis'
r_file = path_to_file + '/data_analysis.R'
args = ['C:/.../test/files', 'C:/.../test/results',
'2', '0.5', '3.5', '3', '5']
call = [r_script, r_file] + args
subprocess.run(call, shell=True)
If the function is called by itself it works completely fine (the .R script is executed and does its job), but as soon as I connect the function to a TkButton the R script opens, but can't import the libraries:
Error in library(ggplot2) : there is no packages called 'ggplot2'
I guess R does not find the libraries anymore...
This is the very basic tkinter GUI (just for testing reasons):
root = tk.Tk()
root.title("Automated Data Analysis")
root.geometry("50x50")
button = tk.Button(master=root, text="Run Analysis", command=call_r)
button.pack()
root.mainloop()
The problem does not occur if call_r() is executed without the tkinter GUI.
Can someone explain what is going on and how to fix this?
Update:
After trying a few different things I was able to narrow down the problem.
As soon as a tkinter function is called in the Python script (e.g. root = tk.Tk() ), the .libPaths() function in R which returns the directory of the installed packages does not work properly anymore.
It returns just one, instead of two directories.
Hope that is somehow useful information...

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

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.

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()

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.

Python Tkinter Menu Command Not Working

I am trying to execute the following code in Python 2.6.5. What I want to do is show a main window with an 'Applications' menu. I want the menu to have a series of commands which should correspond to the keys of the Apps dictionary. When I click the command, I would like the default web browser to open and navigate to the url in the Apps dictionary for that particular key. Instead, when I execute the code the browser is opening to the first url in the Apps dictionary without any clicking. Help please!
from Tkinter import *
import webbrowser
#Real links are to pages on Intranet.
Apps={
'Google':'http://www.google.com/',
'Yahoo':'http://www.yahoo.com/'
}
def openApp(appURL):
webbrowser.open(appURL, new=1, autoraise=1)
return None
root=Tk()
menubar=Menu(root)
root.config(menu=menubar)
appsMenu=Menu(menubar)
for app in Apps:
appsMenu.add_command(label=app, command=openApp(Apps[app]))
menubar.add_cascade(label='Apps', menu=appsMenu)
root.mainloop()
appsMenu.add_command(label=app, command=openApp(Apps[app]))
Command parameters that call functions need to be wrapped in a lambda, to prevent them from being called right away. Additionally, commands bound within a for loop need the looping variable as a default argument, in order for it to bind the right value each time.
appsMenu.add_command(label=app, command=lambda app=app: openApp(Apps[app]))

Categories