I have a translation application for a niche language.
If the user selects/highlights text (typically in the browser) I want the translation func to get triggered. It manually checks and if the language is detected it translates.
I do have my onMouseUp Listener that triggers. However I don’t know how to get the selected text, how to put it into a variable (without triggering ctrl+c and possibly overwriting the users clipboard)
Iam not sure what I am trying to do is possible.
Is it possible to get the users selected/highlighted text in an other app (browser), without triggering ctrl+c and clipboard?
Used pyperclip package however since it uses clipboard it overrides the users current clipboard text which might be important to the user.
So it is not a problem for you to use the clipboard, provided you can keep the old clipboard -- right? So would it be possible for you to save the old clipboard first. Do whatever you want to do and then later restore the clipboard?
Some ideas when using Windows: How do I read text from the clipboard? From there specifically this one could help gross-platform.
I want the user to enter something in the TextBox, and if he exits the program and returns to open the program again at any time, he finds that the words he entered are still there.
(Python programmers - Save Dialog- tkinter )
If you want to save the state of the program, you could save it in file, in a database, or put your data in one place and use pickle. And at least reload the state on program start.
The people who are familiar with the Live Server of VS Code, would have easily understood what is the main motive of this question.
But for others, here's the explanation:
Main motive of Live Server is to Automatically Reload Your Site on Save in web development! (Which get changed for python tkinter).
When ever I change something in my python file which contains tkinter code, the change should be reflected in the main window (the main window should not re-open to reflect the changes).
I have tried to search on web as well as on stack over flow, but all the results are for updating value in entry, label, buttons etc. But what I want is, the whole window should be updated when ever I change something in my main file, and the main window should not be reopened to do so. So in short, updating whole window without closing it, on every changes in the main file or automatically reload your program on save without reopening!
What have I tried?:
I tried to detect change in file using os.getsize which satisfied the first part of my question, but however I am not able to solve the second part i.e window should not be closed.
import os
main__tkinter_filename="myfile.py"
initial_filesize=os.path.getsize(main_tkinter_filename) # Getting size of the file for
# comparison.
while 1:
final_filesize=os.path.getsize(main_tkinter_filename)
if final_filsize<intial_filesize or final_filesize>initial_filesize:
webbrowser.open(main_tkinter_filename)
Example:
from tkinter import *
root=Tk()
root.mainloop
results in the below GUI:
If i have added a=Label(text='text')anda.pack() after root=Tk(), it should show me the label, and if i have removed the same code, it should remove them.
I will answer your question by the best of my understanding,
I have some (a few projects of my own, still way too limited) experience with flutter which has hot-reload feature (same as you described above, which you want with python, mainly tkinter), I recently switched to python for gui (Loved it!), so I would like to share my research here:
I was successfully able to set up hot-reload both with kivy (kivymd hot reload, which comes with watchdog and kaki, which works real-time), and with tkinter, while there is a hitch with the later, you will have to press Ctrl + R as to reload the tkinter window, but it works without having to re-run the python program, I will leave the link to the found resources here, hope it helps with your query!
To setup hot-reload with tkinter (requires Ctrl + R), please refer here.
To setup hot-reload with kivy/kivymd (real-time), which I personally prefer, you can find the official docs here.
To mention, I use the above on Manjaro (Arch linux) with pycharm, atom, but I have also tried and have made it run successfully on Windows 10 with vs code (worked like charm)
Hope I could be of help! If you face any problem regarding the same, please feel free to ask! Thanks!
After digging around I have finally found out a way to implement hot reload feature (which #Stange answers provides) but just updating the selected frame or code.
The basic idea is constanly reading the file and executing the selected code, and removing the object in a list which are meant to be removed.
# Live Checker.py
import keyboard
while 1:
if keyboard.is_pressed("Ctrl+r"):
with open('test.py','r') as file:
file_data=file.read()
file_data_start_index=file_data.find("'#Start#'")
file_data_end_index=file_data.find("'#End#'")
exec_command=file_data[file_data_start_index:file_data_end_index]
with open('exec_log.txt','w') as txt_file:
txt_file.write(exec_command)
Here I am constantly checking if if ctrl+r key is pressed, and if pressed
it reads the file,
writes the selected code from the file into a txt file.
I have specified the start and end of the code to be updated by #Start# and #End# respectively.
# Main.py
def check():
with open('exec_log.txt','r') as exec_c:
exec_command=exec_c.read()
if len(exec_command)==0:
pass
else:
print(exec_command)
exec('for i in root.winfo_children():i.destroy()\n'+exec_command)
print('exec')
with open('exec_log.txt','w') as exec_c:
pass
root.update()
root.after(100,check)
root.after(100,check)
And in the main file, i have added the above code which continusly check if exec_log.txt file has any changes, and if changes are there, then it executes them and all so destroys the widget specified in the remove_list.
This is just a temporary solution which in my case helps me to implement the hot reload feature in tkinter.
I'm creating an application which can jump through tkinter files.
The first one is a login screen which has the following definition:
if results:
os.startfile("data\main.pyw")
with open("data\logdat.txt","w") as text_file:
text_file.write(username.get())
root.destroy()
Next one is the main screen where all the menu options are.
Here is one specific button for a "calculator", so when the button is pressed it opens another tkinter file.
def balancecal():
webbrowser.open(r"data\egyenleg.pyw")
root.destroy()
But when initiating this, it closes the program.
I tried opening the main.py file itself, writing out the "\data" part from the balancecal definition, and with that it opened the file, so the file itself is found.. It just won't work after opening it through the default procedure (with going through the login screen too).
Can someone help me out please?
I'm still a beginner here.
Thanks!
I have a Python/pygI program which shows a window with buttons with various characters, primarily the ones that are not found on standard keyboard. (€æÜÄØá¿ etc)
And I have, say, Gedit open. I have made my program's window unfocusable, so the Gedit's window remains in focus when I press a button in my program's window.
My problem is: when I press a button with a character in my program's window, I want it to be automatically typed into Gedit's window. So it's some kind of an auxiliary on-screen keyboard with rare symbols.
My current method of doing it is via clipboard, but it's tedious because
it contaminates the clipboard
I have to Ctrl+V every time to put the symbol into Gedit.
Is there a way to put a symbol into an active window from Python program? Maybe I should use some Linux utilities via subprocessing? Maybe there is even a way to do it via means of Gtk? Or maybe I can manipulate the clipboard somehow so it would put a character there, automatically paste it to Gedit and put the original clipboard contents back?
EDIT1: I am using X window system in Ubuntu 14.04.