I have something like an e-mail storage and I want that the saved information printed out in my Label, but I want that the information stacked among each other.
This is my code, but when I enter another e-mail and password, the current Label text is replaced.
How can I fix that?
def print_data(mail,passwort):
label_list["text"] = str(mail)+" | "+str(passwort)
def save_info():
mail = entry_mail.get()
passwort = entry_passwort.get()
entry_mail.delete(0, tk.END)
entry_passwort.delete(0, tk.END)
print_data(mail,passwort)
you have to store the previous information in a variable. Then
use:
label.configure(text= "previous label" + "new text")
Related
Attempting to create a book issue page.
I get the data (the document variable) displayed as an array of n-ples, and add the borrow/return buttons in the loop with these data fields. However, since I create multiple instances of the same button variable as seen below, I have no way of knowing which one is clicked. Here is the code:
document = self.app.Member(MemberVerification.mem_id, self.app).searchDocument(SearchBooks.inputvalues, "Books")
logger.info("Results: " + str(document) + " for string filters: " + str(filters))
cells={}
for i in range(len(document)): # Rows
self.borrow = tk.Button(self, text="Borrow", command= lambda: self.checkBorrows(document[i], self.controller, i))
self.retbook = tk.Button(self, text="Return")
for j in range(1, len(document[1])): # Columns
try:
b = tk.Entry(self,justify=tk.CENTER)
b.grid(row=i, column=j)
b.insert(tk.END, str(document[i][j]))
cells[(i, j)] = b
except Exception as e:
logger.error("Error in populateTable: " + str(e) + traceback.format_exc())
sys.exit(-1)
if i!=0:
self.borrow.grid(row=i, column=j+1)
self.borrowbuttons.append(self.borrow)
self.retbook.grid(row=i, column=j+2)
A little explanation - i == 0 is where the column names come in. The button in question is self.borrow. I tried maintaining a list of them to search which button it is, but that didn't work. I would like to be able to maintain a reference to the specific button out of the 3 that have been created, so that the callback function checkBorrows can know which book has to be borrowed. The i parameter was just experimentation, doesn't seem to pass the index in due to the async nature of the button click listener
Any suggestions? I tried maintaining a class variable, but that always ends up referring to the last button, since the callback is technically async.
Thanks!
I'm new to Python3 and coding. I'am get stuck with a simple function.
import datetime
date = str(datetime.date.today())
mail = ""
text = (f"""
some text {mail}
some text {date}
some text
""")
print(text)
def get_deadline_date():
mail = "a#a.com"
print(text)
And I have
some text
some text 2019-03-21
some text
some text
some text 2019-03-21
some text
I can't change the text variable.
It should print:
some text a#a.com
some text 2019-03-21
some text
As I understand, I made a simple mistake, but I can't figure it out.
I tried,
import datetime
date = str(datetime.date.today())
mail = ""
text = (f"""
some text {mail}
some text {date}
some text
""")
print(text)
def get_deadline_date():
global mail
mail = "a#a.com"
get_deadlin
It gave the same output.
Your function is defined but not executed. And, if it was executed, it would not change the value of mail because it has it's own namespace of variables, so it could access mail but not set it. What you are doing there is declaring a new variable, also called mail in that function. On top of that, your text is already set and will not update when you chnge the value of mail. I suggest this solution:
text = "some text {mail}\n" \
"some text {date}\n" \
"some text"
def make_text(text, date):
mail = "a#a.com"
return text.format(mail=mail, date=date)
text = make_text(text=text, date=date.today())
You may also want to make separate functions for setting mail and making the text, but remember to return the new mail you make to the outer scope.
Mistake 1: You are not calling the function
Mistake 2: mail is local variable (to function)
Mistake 3: String is already evaluated/initialised
Bad, but your solution should be
import datetime
date = str(datetime.date.today())
mail = ""
text = """
some text {0}
some text {1}
some text
""".format
print(text(mail, date))
def get_deadline_date():
global mail
mail = "a#a.com"
get_deadline_date()
print(text(mail, date))
Avoid global variable
Dedesign your code to avoid global variable, use function parameters and return values instead.
Currently with the below code I seem to be getting a weird issue as well cant seem to get the value of refreshtoken when I click the submit button. I do get the print for word but for refreshtoken I receive .!entry in the Console.
def getCommand(r):
print('word')
print(r)
tokenWindowFrame = Tk()
tokenWindowFrame.title("Add Character")
refreshLabel = ttk.Label(tokenWindowFrame, text="Refresh Token : ")
refreshLabel.grid(row=1,column=1)
refreshToken = ttk.Entry(tokenWindowFrame, width = 50)
refreshToken.grid(row=1,column=2)
button = ttk.Button(tokenWindowFrame, text = "Submit", command=lambda
r=refreshToken: getCommand(r))
button.grid(row=3,column=2)
tokenWindowFrame.mainloop()
You can't print an entry object, you need to print the text in the entry object. Use:
refreshToken.get()
I ended up having to change this line
button = ttk.Button(tokenWindowFrame, text = "Submit", command=lambda r=refreshToken: getCommand(r))
To :
button = ttk.Button(tokenWindowFrame, text = "Submit", command=lambda r=refreshToken: getCommand(r.get()))
The r.get() is what I was missing. As both print(r.get()) would not work in the Function.
To get the text in an entry box you need:
<entry_box>.get()
If you are trying to print it then you could just do:
print(<entry_box>.get())
Or in your case:
print(r.get())
Hope this works for you!
I'm using WebDriiver, lettuce and python for testing. In first scenario I verifay, that user can create anaccount. For user name I use an e-mail generaror with timestamp.
the lettuce step is:
Then input text "NEW_EMAIL" to text field with ID "s-txt-email"
the python code is:
#step('input text "([^"]*)" to text field with ID "([^"]*)"')
def input_with_id(step, txt, ID):
global new_email
links = get_driver().find_elements_by_xpath("//input[#id='%s']" % ID)
if links:
links[0].click()
else:
raise ValueError('Link with ID %s not found' % ID)
if not "generated_email" in txt:
timestamp = datetime.now().strftime('%m_%d_%Y.%H_%M')
new_email = "automation.%s#yopmail.com" % timestamp
txts = {"NEW_EMAIL": new_email,
"STANDARD_PSW": "xxxxx",
"NEW_USER": new_email,
}
if txt in txts.keys():
txt = txts[txt]
else:
txt = new_email
links[0].send_keys(str(txt))
In other scenario I'm using the same code for verify that user can loggin.
Lettuce step is:
Then input text "generated_email" to text field with ID "l-txt-email-address"
And python code is the same (see above).
But it's generated new e-mail with new timestamp and user can't loggin. What I'm douing wrong? How I can use global variable from one scenario in other one?
I am trying to create a button that adds a row to a table (QtTableWidget) and uses a dialog box to ask for the name, and I have hit a big problem (seemingly a flaw within PyQt).
By adding a row using the insertRow() function the row header automatically has a value of none, which then means you cannot use the verticalHeaderItem(rowPosition).setText(...) on the row Header as it cannot set the text of an item with value none.
The relevant code is here:
def RenameRow(self, i, name):
self.tab1table.verticalHeaderItem(i).setText(name)
def DatabaseAddRow(self):
text, ok = QInputDialog.getText(self, "Row Entry", 'Please Enter A Row Name:', QLineEdit.Normal, 'e.g. ECN 776')
if ok and text != '':
rowPosition = self.tab1table.rowCount()
self.tab1table.insertRow(rowPosition)
self.RenameRow(rowPosition, text)
Any Ideas how to get around this or maybe methods I do not know about?
So I managed to solve this myself just after asking this after wasting half a day on this problem, such is life. The solution to the problem is to assign an empty item to the header and then rename it, the implementation is here:
def RenameRow(self, i, name, table):
item = QTableWidgetItem()
table.setVerticalHeaderItem(i, item)
item = table.verticalHeaderItem(i)
item.setText(QCoreApplication.translate("MainWindow", name))