wxPython message dialog won't work with function - python

I'm new to Python and just started writing a basic GUI program with wxPython. I have a series of text boxes where the user enters data and then they click a submit button.
The submit button triggers a getvalue method for each box (it looks like a=self.textbox1.GetValue()).
The there is a function that simply reads answer=a+b+c+d+e+f+g+h.
Then finally there's the wx.MessageDialog(self, answer, Title, wx.OK | wx.ICON_EXCLAMATION) that prints the answer in a msg dialog.
But instead of printing the sum of the numbers, it just prints them in a series.
I was messing around and replaced the variables in the answer function with actual integers and it gives me an error that says:
String or Unicode type required
I can't really think of any way to fix it since I only have like two days experience with Python.
How can I fix this?

GetValue() gives you the string value of what was typed.
You want to convert the strings to integers before trying to sum them.
a = int(self.textbox1.GetValue())

Related

Qtile: Make parse function for long texts like browsers

I have the following problem.
The widget "WindowName" displays very long Browser names (ie "Stackoverflow | Questions | Ask -- Mozilla Firefox)
Is there a way to shorten this? I am aware that qtile comes with a parse function but it's default function doesnt work
The following function solved this issue, however if someone has a better implementation I would greatly appreciate it.
Add this function to your config.py file
def longNameParse(text):
for string in ["Chromium", "Firefox"]: #Add any other apps that have long names here
if string in text:
text = string
else:
text = text
return text
And then when you call your Window Name widget, you use the function as an argument
widget.WindowName(parse_text=longNameParse)
That fixed it

How to make pywinauto faster (keyboard.send_keys)?

pwa.keyboard.send_keys ("StackOverflow")
with this code, the text is printed 1 letter at a time (long)
how to make it so that the text is entered completely at once? (works with 1000+ variables)
You should be able to change the optional parameter pause to zero like this:
pwa.keyboard.SendKeys("StackOverflow", pause=0)
There are more details on this here

How can I get an input between two print() functions without going to the next line?

I am trying to build this receipt kind of structure.
This is my code:
print('--------------------------------')
print('|\tGEOMETRICAL FIGURE\t|')
print('|\t CALCULATIONS\t|')
print('--------------------------------')
print('|\tFigure:\t\t\t|')
print('|\t1. Circle\t\t|')
print('|\t2. Triangle\t\t|')
print('|\t3. Ellipse\t\t|')
print('|\t4. Trapezium\t\t|')
print('--------------------------------')
print('|\tType number desired\t|')
print('|\t\t',end = '');num = int(input());print('\t\t|')
print('--------------------------------')
I am getting this as the output (5 is the user input I gave):
How can I get those into one single line?
I don't think you can. The new line is what the user types, it doesn't come from your program.
To have more fine control over what's on the terminal you need to use curses, that's much more complicated to use, but I think it's the way to go, so you can handle all cases, including errors.
Another way is to turn off the echo in the terminal, so that the typed stuff will not appear on screen. However you'll have to read digit by digit instead of using input and display it or the user won't see anything.
Try to use one print instead.
print(f"|\t\t{int(input())}\t\t|")

How do I add an error message to a question box?

I am trying to make a revision material for classmates, and I have a list of terms for OCR Computer Science, in a dictionary named "my_dict". However, if a term is entered incorrectly, it just sends an error message to the Python shell. If anyone can help me add an error message to the code provided, that would be much appreciated.
I have tried basic if, while, next loops, but to no avail.
def button_click():
typed_text = (entry1.get()).lower()
output_text.delete(0.0, END)
if typed_text is in my_dict{}:
meaning = my_dict[(typed_text)]
else:
meaning = str("Are you sure you entered the term correctly?"
output_text.insert(END, meaning)
I expect the output to fill the output box with the error message "Are you sure you entered the term correctly?", but the actual output is an invalid syntax currently.
my_dict{} is not a valid syntax, you just have to pass my_dict for the in expression. is in is also not valid syntax, so just use in
In the str("Are you sure you entered the term correctly?" you are missing a paranthese at the end.
There are some spots where you don't need extra parantheses. Ditch them in (entry1.get()) and [(typed_text)]

In SikuliX, type(variable) always types the variable then presses enter automatically. How do I avoid pressing enter?

Sorry if this is really basic, I cannot find a workaround. I have a variable called doc that stores the number 510 that was copied from an excel cell.
I need to type it in a field, but I need to continue typing in another field on the same page afterwards.
My code has:
type(doc)
The log shows:
[log] TYPE "510#ENTER."
The full code looks like this:
type(doc)
wait(1)
type(Key.DOWN)
type(Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE+Key.BACKSPACE)
wait(1)
type(code)
However, I can't get to the type(code) because it switches page before I get there...
Using paste() maybe solved your issue here but this is not the right way to do that as Sikuli does not automatically presses any buttons.
Your problem is probably with the doc variable itself. In your case, you probably just copied the new line character with your variable from excel and that's why Sikuli is hitting Enter. To avoid that, try stripping the new line from your variable prior to typing it, like this:
doc.rstrip()
Then do your usual type(doc) and it should be fine.
Another thing that works is: doc.strip()
It turns out sikuli writes /n after strings, so strip removes that /n.

Categories