Answer the question
In order to leave comments, you need to log in
How to switch language via radibutton?
All good. Just started learning programming. There is a python code like this:
i
import translators as ts
import pyperclip
import keyboard
while True:
keyboard.wait("ctrl+alt+t")
print('переводим:')
bufer = pyperclip.paste()
print(bufer)
perevod = ts.google(bufer, to_language='ru')
pyperclip.copy(perevod)
print(perevod)
from tkinter import *
def sel():
selection = var.get()
label.config(text = selection)
root = Tk()
var = StringVar()
R1 = Radiobutton(root, text="русский", variable=var, value='ru', command=sel).pack( anchor = W )
R2 = Radiobutton(root, text="английский", variable=var, value='en', command=sel).pack( anchor = W )
R3 = Radiobutton(root, text="японский", variable=var, value='ja', command=sel).pack( anchor = W )
label = Label(root)
label.pack()
root.mainloop()
Answer the question
In order to leave comments, you need to log in
It doesn't work for a very obvious reason.
As the name suggests, root.mainloop() enters an infinite loop until the window is closed. This is considered the end of the program, and only then mainloop() returns.
And pressing of the button at you too is implemented through an infinite loop. Of course, only one of them can work.
I would advise you to read the docks on keyboard more closely, and throw out the while True loop and the keyboard.wait () call.
Instead, either try adding a handler via keyboard.add_hotkey() or try scheduling your function call via root.after().
Inside this function, use keyboard.is_pressed() to find out if a key is pressed. There will be another pack of pitfalls - you will need to re-schedule the call to the same function, you will need to ignore keystrokes if the translation has already been done (so that while you hold the keys, the computer does not translate continuously), etc.
The first method is easier, but you need to check if it works.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question