How to make a cursor in a PyQt5 label - python

I have a PyQt5 window that has a label with text in it.
The text that appears there is from a python string variable. The string is built by the user clicking on on-screen pushbuttons and then the string is outputted to the window in that label.
As of now I have a backspace button which deletes the last character in the string, but I would also like the user to be able to click on a spot in front of a character in the label, and then be able to delete that character.
So, I would like to know how to do two things.
how to get the character location in the string based on the user click
I've seen some examples for this, but I'd like to also show a cursor in that spot once the user has clicked there.
I would like to do this with a label widget - not with a text input field.
Anyone have any ideas?

Making a QLabel work like that is hard (QLabel is more complex than it looks).
It is possible to show the cursor after clicking, and that's achieved by setting the textInteractionFlags property:
self.label.setTextInteractionFlags(
QtCore.Qt.TextSelectableByMouse | QtCore.Qt.TextSelectableByKeyboard)
The first flag is to allow handle of mouse events, while the second allows displaying the cursor as soon as the label has focus (for instance, after clicking it).
Unfortunately, this doesn't allow you to get the cursor position (nor to change it); there are ways (using QFontMetrics and QTextDocument), but you need a complex implementation in order to make it really reliable.
The solution is to use a QLineEdit and override the keyPressEvent, which is the function that is always called on a widget whenever a key press happens (and it has input focus). Considering that number input seems still required, just ensure that the event.key() corresponds to a Qt.Key enum for numbers, and in that case, call the base implementation.
You can even make it look exactly like a QLabel by properly setting its stylesheet.
class CommandLineEdit(QtWidgets.QLineEdit):
allowedKeys = (
QtCore.Qt.Key_0,
QtCore.Qt.Key_1,
QtCore.Qt.Key_2,
QtCore.Qt.Key_3,
QtCore.Qt.Key_4,
QtCore.Qt.Key_5,
QtCore.Qt.Key_6,
QtCore.Qt.Key_7,
QtCore.Qt.Key_8,
QtCore.Qt.Key_9,
)
def __init__(self):
super().__init__()
self.setStyleSheet('''
CommandLineEdit {
border: none;
background: transparent;
}
''')
def keyPressEvent(self, event):
if event.key() in self.allowedKeys:
super().keyPressEvent(event)
Then, if you want to set the text programmatically, also based on the cursor, here's a basic usage:
from functools import partial
class CommandTest(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout(self)
self.commandLineEdit = CommandLineEdit()
layout.addWidget(self.commandLineEdit)
keys = (
'QWERTYUIOP',
'ASDFGHJKL',
'ZXCVBNM'
)
backspaceButton = QtWidgets.QToolButton(text='<-')
enterButton = QtWidgets.QToolButton(text='Enter')
self.shiftButton = QtWidgets.QToolButton(text='Shift', checkable=True)
for row, letters in enumerate(keys):
rowLayout = QtWidgets.QHBoxLayout()
rowLayout.addStretch()
layout.addLayout(rowLayout)
for letter in letters:
btn = QtWidgets.QToolButton(text=letter)
rowLayout.addWidget(btn)
btn.clicked.connect(partial(self.typeLetter, letter))
rowLayout.addStretch()
if row == 0:
rowLayout.addWidget(backspaceButton)
elif row == 1:
rowLayout.addWidget(enterButton)
else:
rowLayout.addWidget(self.shiftButton)
spaceLayout = QtWidgets.QHBoxLayout()
layout.addLayout(spaceLayout)
spaceLayout.addStretch()
spaceButton = QtWidgets.QToolButton(minimumWidth=200)
spaceLayout.addWidget(spaceButton)
spaceLayout.addStretch()
backspaceButton.clicked.connect(self.commandLineEdit.backspace)
spaceButton.clicked.connect(lambda: self.typeLetter(' '))
def typeLetter(self, letter):
text = self.commandLineEdit.text()
pos = self.commandLineEdit.cursorPosition()
if not self.shiftButton.isChecked():
letter = letter.lower()
self.commandLineEdit.setText(text[:pos] + letter + text[pos:])
import sys
app = QtWidgets.QApplication(sys.argv)
w = CommandTest()
w.show()
sys.exit(app.exec_())
As you see, you can call backspace() in order to clear the last character (or the selection), and in the typeLetter function there are all the remaining features you required: getting/setting the text and the cursor position.
For anything else, just study the full documentation.

Related

Tkinter : Text highlight appears while pressed but disappear next

I've been working on a Tkinter (Python) project that displays a list of strings using the Text widget recently, but I ran into an issue I couldn't manage to solve :
On startup, I want the first line to be highlighted, and when I click on up/down arrows, the highlight goes up/down, as a selection bar.
I succeed to do that, but the problem is that the highlight only appears when arrows are pressed, and when they are released, it disappear. I'd like it to stay even when I'm not pressing any key.
Here is my code :
class Ui:
def __init__(self):
# the list I want to display in Text
self.repos = repos
# here is the entry keys are bind to
self.entry = Entry(root)
self.entry.pack()
self.bind('<Up>', lambda i: self.changeIndex(-1))
self.bind('<Down>', lambda i: self.changeIndex(1))
# here is the Text widget
self.lists = Text(root, state=NORMAL)
self.lists.pack()
# inits Text value
for i in self.repos:
self.lists.insert('insert', i + '\n')
self.lists['state'] = DISABLED
# variable I use to navigate with highlight
self.index = 0
self.lists.tag_add('curr', str(self.index) + '.0', str(self.index + 1) + '.0') # added + '.0' to make it look '0.0' instead of '0'
self.lists.tag_config('curr', background='#70fffa', background='#000000')
self.root.mainloop()
def changeIndex(self, n):
# error gestion (if < 0 or > len(repos), return)
self.lists.tag_delete('curr')
self.lists.tag_add('curr', str(self.index) + '.0', str(self.index + 1) + '.0')
self.index = self.index + n
# to make it scroll if cannot see :
self.lists.see(str(self.index) + '.0')
I haven't seen any similar problem on Stack, so I asked, but do not hesitate to tell me if it is a duplicate.
Do you guys could help me please ? Thanks !
EDIT: Here is the full code if you want to give it a try : https://github.com/EvanKoe/stack_tkinter.git
EDIT : I added the main.py file (the """backend""" file that calls ui.py) to the demo repository. This way, you'll be able to run the project (be careful, there are "YOUR TOKEN" and "YOUR ORGANIZATION" strings in main.py you'll have to modify with your own token/organization. I couldn't push mine or Github would've asked me to delete my token)
The following code should do what you expect. Explanation below code
from tkinter import *
repos = ["one","two","three","four"]
class Ui:
def __init__(self, parent):
# the list I want to display in Text
self.repos = repos
# here is the entry keys are bind to
self.entry = Entry(parent)
self.entry.pack()
self.entry.bind('<Up>', lambda i: self.changeIndex(-1))
self.entry.bind('<Down>', lambda i: self.changeIndex(1))
# here is the Text widget
self.lists = Text(parent, state=NORMAL)
self.lists.pack()
# inits Text value
for i in self.repos:
self.lists.insert('insert', i + '\n')
self.lists['state'] = DISABLED
# variable I use to navigate with highlight
self.index = 1
self.lists.tag_add('curr', str(self.index) + '.0', str(self.index + 1) + '.0') # added + '.0' to make it look '0.0' instead of '0'
self.lists.tag_config('curr', background='#70fffa', foreground='#000000')
def changeIndex(self, n):
print(f"Moving {n} to {self.index}")
self.index = self.index + n
self.index = min(max(self.index,0),len(self.repos))
self.lists.tag_delete('curr')
self.lists.tag_config('curr', background='#70fffa', foreground='#000000')
self.lists.tag_add('curr', str(self.index) + '.0', str(self.index + 1) + '.0')
# to make it scroll if cannot see :
self.lists.see(str(self.index) + '.0')
root = Tk()
ui = Ui(root)
root.mainloop()
Few changes made to your code
Changed the Ui function to accept the parent tk object as a parameter
Changed self.index to be initialised to 1 rather than 0 since the first line on a text box is 1 not 0
Bound the Up/Down keys to the entry box. Not sure why this is what you are going for but this seems to be what your comments indicate
Added some checking code to limit the index value between 1 and len(repos)
Re-created the tag style each time it is set since you delete the tag (this is why it wasn't showing)
I'd suggest that you look to bind the up/down button press to the text box rather than the entry box. Seems a bit strange to have to select a blank entry box to scroll up and down in a list.
Also, why aren't you just using the build in Tkinter list widget?
I finally managed to solve the problem, and it was due to my event bindings. I made the decision (to improve the UX) to bind up/down arrows on the top Entry instead of binding em on the Text widget. I bind 4 events :
Up arrow => move highlight up,
Down arrow => move highlight down,
Return key => calls get_name(), a function that returns the selected option,
Any other Key => calls repo_filter(), a function that updates the displayed options in the Text widget, according to what has been typed in the Entry.
The problem was that pressing the up/down arrow was triggering "up/down key" event AND "any other key" event, so the highlight was removed since the Text value was refreshed.
To solve this problem, I just had to verify that the pressed key was neither up nor down arrow in the "any other key" event callback :
def repo_filter(evt):
if evt.keysym == 'Up' or evt.keysym == 'Down': # verify that pressed key
return # isn't '<Down>' or '<Up>'
# filter Text widget
Also, I am sorry I didn't give you all the code at the beginning, because, indeed you couldn't guess about those event bindings.
Thanks to everyone who tried to help me !

How to add a second column in a Combobox using Pyqt

I want two columns to be displayed in my Combobox. How could I do it without going too far into code. This code works but I have only achieved it with one column.
sqlcom2="SELECT cod,name FROM product"
data2 = cur.execute(sqlcom2)
result2 = cur.fetchall()
result22 = [i[0] for i in result2]
self.comboBox.addItems(result22)
QComboBox doesn't normally allow to show more than one column of items as its view() is normally a QListView. In order to show more contents, you can use a QTableView instead, but this requires adding more items for each row, so the basic addItem() or addItems() cannot be used, so items must be added to the combobox model instead.
Even if QComboBox currently uses a QStandardItemModel, this might change in the future, so it's better to create your own QStandardItemModel and set that model for the combo.
Then, QComboBox is able to use models with multiple columns (the default is column 0) and the currently used column can be changed using setModelColumn(), but you need to ensure that whenever the user selects an item in a different column the combobox column is update accordingly. In order to do that, a subclass of QTableView is usually the best approach, as it allows to override the methods required to notify the combo about the column change.
There's also another problem: QComboBox uses its own width to automatically resize the popup width. While for single column popup this is not a big problem, if multiple columns are going to be shown, it's possible that only the first column will be shown.
To solve this issue you can set a minimum size for the table view within the showEvent of the table; note that this is generally not a suggested approach, but it is acceptable for this specific case and for the sake of simplicity. Also consider that I'm using the defaultSectionSize() property, and you might prefer to ensure that the columns resize themselves according to the contents, but since that is a more and slightly unrelated topic that might add more complexity to the implementation, I'll not address it in this answer.
class ComboTableView(QtWidgets.QTableView):
def __init__(self, comboBox):
super().__init__()
self.comboBox = comboBox
self.horizontalHeader().hide()
self.verticalHeader().hide()
self.activated.connect(self.updateComboColumn)
def updateComboColumn(self, index):
if (index.isValid() and
index.flags() & QtCore.Qt.ItemIsEnabled and
index.flags() & QtCore.Qt.ItemIsSelectable):
self.comboBox.setModelColumn(index.column())
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.updateComboColumn(self.indexAt(event.pos()))
super().mousePressEvent(event)
def showEvent(self, event):
w = self.horizontalHeader().defaultSectionSize() * self.model().columnCount()
if self.model().rowCount() > self.comboBox.maxVisibleItems():
w += self.verticalScrollBar().sizeHint().width()
self.setMinimumWidth(w)
class SomeWindow(QtWidgets.QMainWindow):
def __init__(self):
# ...
self.comboTableView = ComboTableView(self.comboBox)
self.comboBox.setView(self.comboTableView)
comboModel = QtGui.QStandardItemModel()
self.comboBox.setModel(comboModel)
# ...
sqlcom2="SELECT cod,name FROM product"
data2 = cur.execute(sqlcom2)
result2 = cur.fetchall()
for rowValues in result2:
items = []
for value in rowValues:
items.append(QtGui.QStandardItem(value))
comboModel.appendRow(items)
Finally, you can also implement "horizontal" keyboard navigation within the combo so that the user can switch between columns using the arrow keys, and that can be done by installing an event filter that manages arrow keys.
class SomeWindow(QtWidgets.QMainWindow):
def __init__(self):
# ...
self.comboBox.installEventFilter(self)
def eventFilter(self, source, event):
if source == self.comboBox and event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Right:
column = min(
self.comboBox.modelColumn() + 1,
self.comboBox.model().columnCount() - 1
)
self.comboBox.setModelColumn(column)
elif event.key() == QtCore.Qt.Key_Left:
column = max(0, self.comboBox.modelColumn() - 1)
self.comboBox.setModelColumn(column)
return super().eventFilter(source, event)

How to undo an edit of a QListWidgetItem in PySide/PyQt?

Short version
How do you implement undo functionality for edits made on QListWidgetItems in PySide/PyQt?
Hint from a Qt tutorial?
The following tutorial written for Qt users (c++) likely has the answer, but I am not a c++ person, so get a bit lost: Using Undo/Redo with Item Views
Longer version
I am using a QListWidget to learn my way around PyQt's Undo Framework (with the help of an article on the topic). I am fine with undo/redo when I implement a command myself (like deleting an item from the list).
I also want to make the QListWidgetItems in the widget editable. This is easy enough: just add the ItemIsEditable flag to each item. The problem is, how can I push such edits onto the undo stack, so I can then undo/redo them?
Below is a simple working example that shows a list, lets you delete items,and undo/redo such deletions. The application displays both the list and the the undo stack. What needs to be done to get edits onto that stack?
Simple working example
from PySide import QtGui, QtCore
class TodoList(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.initUI()
self.show()
def initUI(self):
self.todoList = self.makeTodoList()
self.undoStack = QtGui.QUndoStack(self)
undoView = QtGui.QUndoView(self.undoStack)
buttonLayout = self.buttonSetup()
mainLayout = QtGui.QHBoxLayout(self)
mainLayout.addWidget(undoView)
mainLayout.addWidget(self.todoList)
mainLayout.addLayout(buttonLayout)
self.setLayout(mainLayout)
self.makeConnections()
def buttonSetup(self):
#Make buttons
self.deleteButton = QtGui.QPushButton("Delete")
self.undoButton = QtGui.QPushButton("Undo")
self.redoButton = QtGui.QPushButton("Redo")
self.quitButton = QtGui.QPushButton("Quit")
#Lay them out
buttonLayout = QtGui.QVBoxLayout()
buttonLayout.addWidget(self.deleteButton)
buttonLayout.addStretch()
buttonLayout.addWidget(self.undoButton)
buttonLayout.addWidget(self.redoButton)
buttonLayout.addStretch()
buttonLayout.addWidget(self.quitButton)
return buttonLayout
def makeConnections(self):
self.deleteButton.clicked.connect(self.deleteItem)
self.quitButton.clicked.connect(self.close)
self.undoButton.clicked.connect(self.undoStack.undo)
self.redoButton.clicked.connect(self.undoStack.redo)
def deleteItem(self):
rowSelected=self.todoList.currentRow()
rowItem = self.todoList.item(rowSelected)
if rowItem is None:
return
command = CommandDelete(self.todoList, rowItem, rowSelected,
"Delete item '{0}'".format(rowItem.text()))
self.undoStack.push(command)
def makeTodoList(self):
todoList = QtGui.QListWidget()
allTasks = ('Fix door', 'Make dinner', 'Read',
'Program in PySide', 'Be nice to everyone')
for task in allTasks:
todoItem=QtGui.QListWidgetItem(task)
todoList.addItem(todoItem)
todoItem.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
return todoList
class CommandDelete(QtGui.QUndoCommand):
def __init__(self, listWidget, item, row, description):
super(CommandDelete, self).__init__(description)
self.listWidget = listWidget
self.string = item.text()
self.row = row
def redo(self):
self.listWidget.takeItem(self.row)
def undo(self):
addItem = QtGui.QListWidgetItem(self.string)
addItem.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
self.listWidget.insertItem(self.row, addItem)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
myList=TodoList()
sys.exit(app.exec_())
Note I posted an earlier version of this question at QtCentre.
That tutorial you mentioned is really not very helpful. There are indeed many approaches to undo-redo implementation for views, we just need to choose the simplest one. If you deal with small lists, the simpliest way is to save all data on each change and restore full list from scratch on each undo or redo operation.
If you still want atomic changes list, you can track user-made edits with QListWidget::itemChanged signal. There are two problems with that:
Any other item change in the list will also trigger this signal, so you need to wrap any code that changes items into QObject::blockSignals calls to block unwanted signals.
There is no way to get previous text, you can only get new text. The solution is either save all list data to variable, use and update it on change or save the edited item's text before it's edited. QListWidget is pretty reticent about its internal editor state, so I decided to use QListWidget::currentItemChanged assuming that user won't find a way to edit an item without making is current first.
So this is the changes that will make it work (besides adding ItemIsEditable flag in two places):
def __init__(self):
#...
self.todoList.itemChanged.connect(self.itemChanged)
self.todoList.currentItemChanged.connect(self.currentItemChanged)
self.textBeforeEdit = ""
def itemChanged(self, item):
command = CommandEdit(self.todoList, item, self.todoList.row(item),
self.textBeforeEdit,
"Rename item '{0}' to '{1}'".format(self.textBeforeEdit, item.text()))
self.undoStack.push(command)
def currentItemChanged(self, item):
self.textBeforeEdit = item.text()
And the new change class:
class CommandEdit(QtGui.QUndoCommand):
def __init__(self, listWidget, item, row, textBeforeEdit, description):
super(CommandEdit, self).__init__(description)
self.listWidget = listWidget
self.textBeforeEdit = textBeforeEdit
self.textAfterEdit = item.text()
self.row = row
def redo(self):
self.listWidget.blockSignals(True)
self.listWidget.item(self.row).setText(self.textAfterEdit)
self.listWidget.blockSignals(False)
def undo(self):
self.listWidget.blockSignals(True)
self.listWidget.item(self.row).setText(self.textBeforeEdit)
self.listWidget.blockSignals(False)
I would do it like this:
Create a custom QItemDelegate and use these two signals:
editorEvent
closeEditor
On editorEvent: Save current state
On closeEditor: Get new state and create a QUndoCommand that set the new state for Redo and the old state for Undo.
Each time you verify and accept the new text of the item, save it as list item data. Quasi-semi-pseudo-code:
OnItemEdited(Item* item)
{
int dataRole{ 32 }; //or greater (see ItemDataRole documentation)
if (Validate(item->text()) {
item->setData(dataRole, item->text());
} else { //Restore previous value
item->setText(item->data(dataRole).toString());
}
}
I'm sorry if it looks too much like C++.

Qprogressbar with two values

I have some unusual question :
For visualization of packing progress i think about qprogressbar with two values in one bar - one showing bytes read, and another showing write-out bytes, which gives also imagine about compress ratio.
It is possible with QT4 ?
Also, I have very little experience with C++ coding, my current work is based on Python, PyQT4,
Yes it's possible, but you will have to implement your own "DualValueProgressbar" here you have an example, is not complete production code but it will point to you in the right direction.
A note before continue:
Will this you will be able to show two values in the bar, but show two colours in the same bar is a very different thing. So I'll recomend you to use two prograssbar for doing what you want, keep it simple.
Before see any code let me explain what I did.
Subclass QProgressBar
Add a variable member called self.__value_1. This will be the second value.
Override the method paintEvent in order to draw self.__value_1 inside the bar.
Recomendations:
Write code for establishing limits on the second value. (Minimun and maximun)
Write code for handle the format property.
Write code for habdle the aligment property.
This is the result:
Here is the code:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class DualValueProgressBar(QProgressBar):
def __init__(self, parent=None):
super(DualValueProgressBar, self).__init__(parent)
# The other value you want to show
self.__value_1 = 0
def paintEvent(self, event):
# Paint the parent.
super(DualValueProgressBar, self).paintEvent(event)
# In the future versions if your custom object you
# should use this to set the position of the value_1
# in the progressbar, right now I'm not using it.
aligment = self.alignment()
geometry = self.rect() # You use this to set the position of the text.
# Start to paint.
qp = QPainter()
qp.begin(self)
qp.drawText(geometry.center().x() + 20, geometry.center().y() + qp.fontMetrics().height()/2.0, "{0}%".format(str(self.value1)))
qp.end()
#property
def value1(self):
return self.__value_1
#pyqtSlot("int")
def setValue1(self, value):
self.__value_1 = value
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = QWidget()
hlayout = QHBoxLayout(window)
dpb = DualValueProgressBar(window)
dpb.setAlignment(Qt.AlignHCenter)
# This two lines are important.
dpb.setValue(20)
dpb.setValue1(10) # Look you can set another value.
hlayout.addWidget(dpb)
window.setLayout(hlayout)
window.show()
sys.exit(app.exec())
Finally the code sample:

pyside / pyqt: Getting values from dynamically created qlineedits on button clicked

I have a program that creates a number of qlineedits and buttons depending on the users input:
On the image above 4 lines have been added with a button after the grayed out "Next" button was clicked. Now I want to get the input from the user into a function when the corresponding button is clicked (Click "Create Shot 1! --> goto a function with "exShot1" passed as an argument).
The thing is I have no idea how to get the names of each qline and button when they are created in a loop. I guess I could create unique variables in the loop but that doesn't feel right. I have tried using setObjectName but I can't figure out how I can use that to call the text. I also made an unsuccessful attempt with Lamdba (which I have a feeling might be the right way to go somehow) I believe it's a combination of having to fetch the name and tracking when the user input is changed.
I have experimented with textChanged and I got it to work on the last entry of the loop but not for the other qlines and buttons)
Relevant code:
while i <= int(seqNum):
#create each widget
self.createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
self.labelName = QtGui.QLabel(self)
self.labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
self.shotName = QtGui.QLineEdit(self)
self.shotName.setObjectName("shot"+str(i))
#add widget to layout
self.grid.addWidget(self.labelName, 11+shotjump,0)
self.grid.addWidget(self.shotName,11+shotjump,1)
self.grid.addWidget(self.createShotBtn, 11+shotjump,2)
#Press button that makes magic happen
self.createShotBtn.clicked.connect(???)
i += 1
edit: It would also be fine if the user entered input on all the lines and just pressed one button that passed all those inputs as a list or dict (there will be more lines added per "shot")
The problem is that on each run through the values of self.createShotBtn, self.labelName and self.shotName are being overridden.
So on the last run through, they are fixed, but only for the last iteration.
Instead, you want to use a locally scoped variable in the loop, and potentially store it in an array for later use.
This code should come close to what you need, but I can see where self.shotNumberLst (which returns a number?) and shotjump (which is an offest, or equal to to i) are declared.
self.shots = []
for i in range(seqNum): # Changed while to for, so you don't need to increment
#create each widget
createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
labelName = QtGui.QLabel(self)
labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
shotName = QtGui.QLineEdit(self)
self.shots.append({"button":createShotBtn,
"name":shotName)) # Store for later if needed.
#add widget to layout
self.grid.addWidget(labelName, 11+shotjump,0)
self.grid.addWidget(shotName,11+shotjump,1)
self.grid.addWidget(createShotBtn, 11+shotjump,2)
#Press button that makes magic happen
createShotBtn.clicked.connect(self.createShot(i))
#elsewhere
def createShot(self,index):
print self.shots[index]["name"].text
Try this,
while i <= int(seqNum):
#create each widget
createShotBtn = "ShotBtn"+str(i)
self.createShotBtn = QtGui.QPushButton("Create Shot %s!" %str(self.shotNumberLst[i-1]))
labelName = "labName"+str(i)
self.labelName = QtGui.QLabel(self)
self.labelName.setText("Enter Name Of Shot %s!" %str(self.shotNumberLst[i-1]))
shotName = "shtName"+str(i)
self.shotName = QtGui.QLineEdit(self)
#add widget to layout
self.grid.addWidget(self.labelName, 11+shotjump,0)
self.grid.addWidget(self.shotName,11+shotjump,1)
self.grid.addWidget(self.createShotBtn, 11+shotjump,2)
#Press button that makes magic happen
self.createShotBtn.clicked.connect(self.printText)
i += 1
def printText(self):
print(self.shotName.text())
This will print the text when you push the button on the same line.

Categories