Googletrans Error- The handshake operation timed out - python

When I try to translate text using the googletrans module, I get an error:
httpcore._exceptions.ConnectTimeout: _ssl.c:1106: The handshake operation timed out
Here's my code:
from tkinter import *
from googletrans import Translator , LANGUAGES
root = Tk()
root.geometry("500x500")
def translate():
translator = Translator()
translated_text = translator.translate(text=entry.get(),
dest=languages_listbox.get(languages_listbox.curselection())) # Text does not get get translated and it throws an error
output_label.config(text = f"Translated Text: {translated_text}")
entry = Entry(root , font = "consolas 14")
entry.place(x = 120 , y = 0)
entry.insert(0 , "Hello world")
languages_list = []
for key, value in LANGUAGES.items():
languages_list.append(value)
languages_listbox_frame = Frame(root)
languages_listbox_frame.place(x = 120 , y = 50)
languages_listbox = Listbox(languages_listbox_frame , font = "calibri 14")
languages_listbox.grid(row = 0 , column = 0)
scrollbar = Scrollbar(languages_listbox_frame , orient = VERTICAL , command = languages_listbox.yview)
scrollbar.grid(row = 0 , column = 1 , sticky = N+S+E+W)
languages_listbox.config(yscrollcommand = scrollbar.set)
for language in languages_list:
languages_listbox.insert(END , language)
languages_listbox.select_set(0)
translate_button = Button(root , text = "Translate" , font = "comicsansms 18 bold" , command = translate)
translate_button.place(x = 160 , y = 370)
output_label = Label(root , text = "Translated Text: " , font = "comicsansms 16 bold")
output_label.place(x = 5 , y = 460)
mainloop()
My code is very simple and I can't figure out what's wrong with it.
I tried changing the network connection, but it made no difference.
Is there any way to fix this problem?
It would be great if anyone could help me out.

Well that seems very much like an API error as google does not like when you send too much request, I cannot close this question either as the answer there is not an accepted one, anyway here is an alternative using deep_translator.
Start by installing it:
pip install deep-translator
Now I recommend you take a stroll down its docs, but ill include an example using Google Translate anyway. Start by importing it:
from deep_translator import GoogleTranslator
Now create an initial instance of it, just to get the language map.
translator = GoogleTranslator(target='en')
languages_list = []
lang_map = translator.get_supported_languages(as_dict=True) # Get in form of dict
for key, value in lang_map.items():
languages_list.append(key.title()) # Making first letter capital with title()
Now for the translation part, change your function to the following:
def translate():
lang = lang_map[languages_listbox.get(languages_listbox.curselection()[0]).lower()] # Get the corresponding language code from the dictionary
translator = GoogleTranslator(source='en',target=lang) # Create new instance with selected language
translated_text = translator.translate(entry.get()) # Translate
output_label.config(text = f"Translated Text: {translated_text}") # Update the text
Yes creating a new instance every time function is run is not a good idea, but I couldn't find another successful method to change the target language once it is initially set. It does not seem to give any error as of now, unless your source and target languages are both same.
On a side note, it is better to use threading, as sometimes the API might take time to respond and it will freeze the GUI.

Related

<Control-x> binding not working in tkinter text widget

Whenever I try to bind a text widget with "<Control-x>" , I keep getting an error that says:
_tkinter.TclError: PRIMARY selection doesn't exist or form "STRING" not defined
Here's the code:
from tkinter import *
root = Tk()
def cut_text(event):
selected = text.selection_get()
clipboard_label.config(text = clipboard_label['text'] + f"\t{selected}")
text = Text(root , width = 65 , height = 20 , font = "consolas 14")
text.grid(row = 0 , column = 0)
clipboard_label = Label(root , text = "Your Clipboard History:" , font = "arial 11")
clipboard_label.grid(row = 1 , column = 0)
root.bind("<Control-x>" , cut_text)
mainloop()
As I keep getting that error , I tried this:
def cut_text(event):
selected = text.get("sel.first" , "sel.last")
clipboard_label.config(text = clipboard_label['text'] + f"\t{selected}")
But when I do that , I get this error:
_tkinter.TclError: text doesn't contain any characters tagged with "sel"
I don't face this problem with any other binding like "<Control-c>" , but this problem only occurs when I use the "<Control-x>" binding.
Is there any way to solve this problem ?
It would be great if anyone could help me out.
Well you can return 'break' for this purpose. Like:
text.bind("<Control-x>" , cut_text)
root.bind('<Control-x>',lambda e: 'break')
What 'break' does is, it overrides the default behaviour(sort-ish). I could link you to a nicely written documentation, but Effbot is currently down.

Open Tkinter frame based on logical expression result in Python

Hi Tkinter and Python Masters,
Here's my problem. I'm creating a simple GUI with Tkinter. I have a background image that I would like to change based on the value of a variable defined in a function that connects to an API (JSON). The variable will hold one of two values. Either a 1 or a 2.
What I would like to do in Tkinter (which I'm totally lost on) is have a background image. If the value of the variable in my API function is 1 show Image1 as the background. If the value of the variable is 2, show Image2 as the background.
It is a bit messy, but here's my API function
def apiconnect(statusvar):
def to_serializable(ticketid):
return str(ticketid)
url = "http://staging2.apiname.com/ticket_api/tickets"
data = {'ticket_id' : ticketid, 'direction' : 'up'}
headers = {'Content-Type' : 'application/json', 'Authorization' : 'J0XxxxxVRy9hMF9Fo7j5'}
r = requests.post(url, data=json.dumps(data), headers=headers)
requestpost = requests.post(url, headers = headers, json = data)
response_data = requestpost.json()
statusvar = (response_data["status"])
messagevar = (response_data["message"])
json.dumps(url,data)
global accessResult
if statusvar == "successful":
accessResult = 1
else:
accessResult = 2
Is this possible in Tkinter? I basically want to reference my variable accessResult in my Tkinter frame, and change the background image based on that value.
Be gentle with me, I'm all new and such
So I'm no expert with Tkinter but if you want to change the background based on only two values why not use a boolean instead so:
# Here is where we make the check.
if accessResult == True:
background_photo = PhotoImage(file = "C:\\Path\\To\\My\First\\\Photo1.png")
else:
background_photo = PhotoImage(file = "C:\\Path\\To\\My\\Second\\Photo2.png")
backgroundLabel = Label(top, image=backgroundPhoto)
backgroundLabel.place(x=0, y=0, relwidth=1, relheight=1)
Good luck! Hope this helps!

How to store Entry as a variable that can be used in an equation to have the result printed in label with tkinter

I am trying to take entry from an entry boxes and use them in an equation. Basically it is
Entry_one - Entry_two * entry_three / 12. But I cant seem to figure out how to do this. To make it more clear here is the working python code:
a = int(input('Order Amount. '))
c = int(input('Beams Left. '))
l = int(input('Beam Length. '))
a1 = a-c
a2 = a1*l
a3 = a2/12
print ('The Hourly Count Is: ',a3)
Now here is the code that I'm trying to make do the exact same thing with tkinter:
from tkinter import *
root = Tk()
root.geometry('450x450')
root.title('Hourly Production Count')
def calc_result():
subtract_var = int(total_order_ent.get()) - int(beams_left_ent.get())
beams_left_var = int(subtract_var) * int(length_ent.get())
order_output = int(beams_left_var) / 12
answer_var = order_output.get()
label = Label(root,text = answer_var).pack()
button1 = Button(root,text = 'Enter',command = calc_result,fg='black',bg= 'green').pack()
total_order_ent = Entry(root).pack()
Beams_left_ent = Entry(root).pack()
length_ent = Entry(root).pack()
label_total = Label(root,text = 'Enter Total Order Amount').pack()
label_beams_left = Label(root,text = 'Enter Amount Left To Complete').pack()
root.mainloop()
That's what I have so far. I havn't used any grid placement for the widgets yet since I just want to get the code to work before I work on how it looks but if anyone can help me it would be appreciated. I've searched other questions and have modified other code, tried it as a class and other things and just cant seem to make it work. some errors I'm getting are:
line 23, in <module>
label_length = Label('Enter Beam Length').pack()
Python\Python35\lib\tkinter\__init__.py", line 2109, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
Your code doesn't exhibit the error you say it does. Just about the only possible way to get that specific error is if you omit the first three lines of your program after the import, and set root to a string. Even when you do that, you get a slightly different error -- it will complain about the first use of Button rather than Label as in your question.
My guess is, you're not running the exact code you think you are. Regardless, this specific problem is due to the fact you haven't properly created a root window, and you're passing a string as the parent to the other widgets.
Once you solve that problem, you also will need to solve the problem addressed in this question: Tkinter: AttributeError: NoneType object has no attribute get. The short version is, total_order_ent = Entry(root).pack() will set total_order_ent to None, making it impossible to get the value of the widget at a later date.
you could create a variable and retrieve it with the get method in your function:
as explained in the following documentation:
from tkinter import *
root = Tk()
var_01 = StringVar()
enter_01 = Entry(root, textvariable=var_01)
enter_01.pack()
def get_var():
getvar = var_01.get()
print(getvar)
button_01 = Button(root, text='print variable', command=get_var)
button_01.pack()
root.mainloop()

raw_input stops GUI from appearing

I have written a program in Python that allow me to change the names of many files all at once. I have one issue that is quite odd.
When I use raw_input to get my desired extension, the GUI will not launch. I don't get any errors, but the window will never appear.
I tried using raw_input as a way of getting a file extension from the user to build the file list. This program will works correctly when raw_input is not used.The section of code that I am referring to is in my globList function. For some reason when raw_imput is used the window will not launch.
import os
import Tkinter
import glob
from Tkinter import *
def changeNames(dynamic_entry_list, filelist):
for index in range(len(dynamic_entry_list)):
if(dynamic_entry_list[index].get() != filelist[index]):
os.rename(filelist[index], dynamic_entry_list[index].get())
print "The files have been updated!"
def drawWindow(filelist):
dynamic_entry_list = []
my_row = 0
my_column = 0
for name in filelist:
my_column = 0
label = Tkinter.Label(window, text = name, justify = RIGHT)
label.grid(row = my_row, column = my_column)
my_column = 1
entry = Entry(window, width = 50)
dynamic_entry_list.append(entry)
entry.insert(0, name)
entry.grid(row = my_row, column = my_column)
my_row += 1
return dynamic_entry_list
def globList(filelist):
#ext = raw_input("Enter the file extension:")
ext = ""
desired = '*' + ext
for name in glob.glob(desired):
filelist.append(name)
filelist = []
globList(filelist)
window = Tkinter.Tk()
user_input = drawWindow(filelist)
button = Button(window, text = "Change File Names", command = (lambda e=user_input: changeNames(e, filelist)))
button.grid(row = len(filelist) + 1 , column = 1)
window.mainloop()
Is this a problem with raw_input?
What would be a good solution to the problem?
This is how tkinter is defined to work. It is single threaded, so while it's waiting for user input it's truly waiting. mainloop must be running so that the GUI can respond to events, including internal events such as requests to draw the window on the screen.
Generally speaking, you shouldn't be mixing a GUI with reading input from stdin. If you're creating a GUI, get the input from the user via an entry widget. Or, get the user input before creating the GUI.
A decent tutorial on popup dialogs can be found on the effbot site: http://effbot.org/tkinterbook/tkinter-dialog-windows.htm

Displaying Pandas dataframe in tkinter

I'm creating a tkinter gui that will take user input for a variable that then gets passed to SQL and the queried data (in this case a single column data frame and boxplot). However, at this moment I can not find a means of displaying my pandas dataframe in the tk gui. I have not found any module or means of displaying this, and I've spent hours going through possible solutions to no avail. Only thing I need is for the dataframe to display in the gui and be re-rendered each time I change the dataframe through the user input function. My code atm is:
### Starting first GUI/Tkinter Script
##First load libs
import pyodbc
import numpy as np
import pandas.io.sql as sql
import pandas
import matplotlib.pyplot as plt
import pylab
import matplotlib as mpl
from tkinter import *
import sys
plt.ion()
def userinput():
global PartN, pp, df
##a = raw_input(v.get())
a = E1.get()
##print a
PartN = a
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=my server;
DATABASE=PackingList;UID=myid;PWD=mypword')
sqlr = "SELECT partmadeperhour FROM Completions WHERE PartNumber = ?
AND endtime > '2012-12-31 23:59:00' ORDER BY partmadeperhour"
df = pandas.read_sql_query(sqlr, conn, params=[PartN])
conn.close()
print(df)
stats = df['partmadeperhour'].describe()
print(stats)
print("Part Name is %s" %PartN)
##df.plot(kind='box', fontsize='20')
pp = df.plot(kind='box', fontsize='20')
plt.show(block=True)
def clear_textbox():
E1.delete(0, END)
master = Tk()
master.title('Title')
v = StringVar()
PartN = None
L1 = Label(master, text = 'Name')
L1.pack(side = LEFT)
E1 = Entry(master, textvariable = v, bd = 5)
E1.pack(side = RIGHT)
b = Button(master, text = 'Submit', command = userinput)
b.pack(side = TOP)
b2 = Button(master, text = 'Clear', command=clear_textbox)
b2.pack(side=BOTTOM)
master.mainloop()
An example of my data frame
rate
0 [0.25]
1 [0.67]
2 [0.93]
... ...
1474 [5400.00]
If someone could just point me in the right direction, I don't even need code corrections just yet, I just need to hear someone to say yes it is possible (which I know it is), and to give me some kind of example. Thanks
I don't have enough rep to comment, otherwise I would, but I used this video to get a grasp of interacting with pandas/numpy/matplotlib and tkinter. He ends up building a large application with it if you stick through all the videos.
Even though you aren't likely doing the exact same thing, I think there might still be useful methods of interacting with your data and GUI gleamed from the videos.
If the link ever dies, you can search on YouTube "Tkinter tutorial sentdex"
Best of luck.

Categories

Resources