Python EasyGui : Returning User Input In TextBoxes - python

Hello i am currently using python 2.7 to create a GUI based program with the add on library EasyGui. I'm trying to take the user input from a multiline textbox and print those values to another function which displays inside a messagebox. currently my code looks like this:
fieldNames = ["Name","Street Address","City","State","ZipCode"]
fieldValues = []
def multenterbox123():
multenterbox(msg='Fill in values for the fields.', title='Enter', fields=(fieldNames), values=(fieldValues))
return fieldValues
multenterbox123();
msgbox(msg=(fieldValues), title = "Results")
its currently returing a blank value in the messagebox (msgbox) and i understand why its doing this , as its pointing to the blank list variable fieldValues. I actually want to take the list values after its passed in from the user in the multi line textbox (multenterbox123) function, but im having trouble trying to work out how to best implement this.
Any help into this would be greatly appreciated as im only new to python programming (:

from easygui import msgbox, multenterbox
fieldNames = ["Name", "Street Address", "City", "State", "ZipCode"]
fieldValues = list(multenterbox(msg='Fill in values for the fields.', title='Enter', fields=(fieldNames)))
msgbox(msg=(fieldValues), title = "Results")
I tested the code above on my computer and the msgbox returned what I entered in the multenterbox . There is an example in the documentation if you want to take a look at it. Multenterbox-EasyGUI-Documentation. Basically you first need to make a list, hence the list function. And all the values entered will be stored in it. So whatever I write in the multenterbox will be saved in the fieldValues list.

Related

How to use the most recently printed line as an input?

I am trying to create a Twitter bot that posts a random line from a text file. I have gone as far as generating the random lines, which print one at a time, and giving the bot access to my Twitter app, but I can't for the life of me figure out how to use a printed line as a status.
I am using Tweepy. My understanding is that I need to use api.update_status(status=X), but I don't know what X needs to be for the status to match the most recently printed line.
This is the relevant section of what I have so far:
from random import choice
x = 1
while True:
file = open('quotes.txt')
content = file.read()
lines = content.splitlines()
print(choice(lines))
api.update_status(status=(choice(lines)))
time.sleep(3600)
The bot is accessing Twitter no problem. It is currently posting another random quote generated by (choice(lines)), but I'd like it to match what prints immediately before.
I may not fully understand your question, but from the very top, where it says, "How to use the most recently printed line as an input", I think I can answer that. Whenever you use the print() command, store the argument into a string variable that overwrites its last value. Then it saves the last printed value.
Instead of directly printing a choice:
print(choice(lines))
create a new variable and use it in your print() and your api.update_status():
selected_quote = choice(lines)
print(selected_quote)
api.update_status(status=selected_quote)

Issue with a python string loop

So I'm having trouble getting my code to use a list of strings as inputs in a loop. Here's roughly what I have so far.
from arcgis.gis import GIS
Users = ['User01','User02','User03']
User_string = str(Users) # Have to do this as code needs input as string
gis = GIS("https://www.arcgis.com","USERNAME","PASSWORD") # This logs you into ArcGIS Online
User_role = 'org_user'
for x in User_string:
test = gis.users.get(username=x)
test.update_role(role=User_role)
print("Done! Check Web")
I just can't get the loop to work right. When I remove the for loop and put each user name in individually the get user and update role commands work just fine, it's just in the loop that is broken.
The two errors I'm getting is that the username has to be a string. I fixed that by adding the str() command, but I can't get the username to enter into the user.get loop.
Any suggestions? This code is actually looking at an excel file to produce the list of usernames so I can't just hardcode the list into the code. If it helps at all the website I've been using for the ArcGIS portion of the code is this one: https://developers.arcgis.com/python/guide/accessing-and-managing-users/
I should mention that I also tried just printing
test=gis.users.get(username=User_string)
And it came back as None. So I guess my question is how do I get 'User01' to go into the username=x spot?
Thanks much!
You're doing a for with the list as a string so it's looping on each character of the string. You need to do it with the original list.
Based on your code, you are telling your for loop to iterate on a String, since you converted your list to a string with User_string = str(Users); So the loop is going over each character on the string, which now it is User01User02User03
What you need to do is to iterate the list Users, like:
from arcgis.gis import GIS
Users = ['User01','User02','User03']
gis = GIS("https://www.arcgis.com","USERNAME","PASSWORD") # This logs you into ArcGIS Online
User_role = 'org_user'
for x in Users:
test = gis.users.get(username=x)
test.update_role(role=User_role)
print("Done! Check Web")

How to combine two strings to describe a property of tree.insert?

I'm creating a gui with tKinter, working with python for the first time.
Part of my gui is a treeview, the nodes in the treeview have images attached.
I made a function to add new nodes to the treeview.
I want to add an image to the new node based on the mother of the node.
In this case The variable 'curItem' returns the mother as a string, "test" in this case.
I want to combine the string "photo_" and "test" and use this in the 'tree.insert' code.
But for this to work I have to convert the string to something else, but I dont know to what and how to do this.
This is probably a very basic question, but I have been unable to find an answer so far.
Part of the relevant code:
photo_test = PhotoImage(file="resources/test.png")
def add():
curItem = tree.selection()[0] #returns "test"
img = "photo_" + curItem
tree.insert(curItem, 'end', text='new', image=img) #doesn't work
tree.insert(curItem, 'end', text='new', image=photo_test) #works
You're trying to set the image to the string 'photo_test'. Try storing the actual photo in a dict and access it via the string, something like this.
photos = dict()
photos["photo_test"] = PhotoImage(file="resources/test.png")
def add():
curItem = tree.selection()[0] #returns "test"
img = "photo_" + curItem
tree.insert(curItem, 'end', text='new', image=photos[img])
You seems to have misunderstood the difference between a variable and a string though. A string is just text within the code, not actually code, so you can't pass a variable name in string form and expect the code to read that value. "photo_test" is not the same as photo_test.

Building a ranking list in Python: how to assign scores to contestants?

(I posted this on the wrong section of Stackexchange before, sorry)
I'm working on a assignment which is way above my head. I've tried for days on end figuring out how to do this, but I just can't get it right...
I have to make a ranking list in which I can enter a user, alter the users score, register if he/she payed and display all users in sequence of who has the most score.
The first part I got to work with CSV, I've put only the basic part in here to save space. The menu and import csv have been done: (I had to translate a lot from my native language, sorry if there is a mistake, I know it's a bad habit).
more = True
while more:
print("-" * 40)
firstname = raw_input("What is the first name of the user?: ")
with open("user.txt", "a") as scoreFile:
scoreWrite = csv.writer(scoreFile)
scoreWrite.writerow([firstname, "0", "no"])
scoreFile.close()
mr_dnr = raw_input("Need to enter more people? If so, enter 'yes' \n")
more = mr_dnr in "yes \n"
This way I can enter the name. Now I need a second part (other option in the menu of course) to:
let the user enter the name of the person
after that enter the (new) score of that person.
So it needs to alter the second value in any entry in the csv file ("0") to something the user enters without erasing the name already in the csv file.
Is this even possible? A kind user suggested using SQlite3, but this basic CSV stuff is already stretching it far over my capabilities...
Your friend is right that SQlite3 would be a much better approach to this. If this is stretching your knowledge too far, I suggest having a directory called users and having one file per user. Use JSON (or pickle) to write the user information and overwrite the entire file each time you need to update it.

python text widget get method

I have a text widget in my python Tkinter script and i am trying to get the value that the user enter. My intention is to write the data from the text widget together with other values from the script(ie. x,y,z) to the txt file(faultlog.txt) as a single line with semi-column separated. This is what i tried.
...
text=Text(width=30,height=1)
text.place(x=15,y=75)
data=text.get(1.0,END)
lines=[]
lines.append('{};{};{};{} \n'.format(data,x,y,z))
faultlog=open("faultlog","a")
faultlog.writelines(lines)
faultlog.close()
...
Instead of giving me a single line output in the text file, python is writing this to the txt file (assuming the data that user enter is "abcdefgh")
abcdefgh
;x;y;z
just to make things clear, this is what i want
abcdefgh;x;y;z
What did i do wrong? I hope the question is clear enough, i am a beginner so please make the answer simple.
When you get all text of the widget, there is also included a "\n" at the end. You can remove this last character like this:
data=text.get(1.0,END)[:-1]
Note that this always works independently of the length of the text length:
>>> "\n"[:-1]
''
>>> ""[:-1]
''

Categories

Resources