Issue
I am still pretty new to python and the application I am writing I really want to keep the window open and change the text of a label when OK is selected. The text is stored in a config file, which is not the issue. I can bring it into a list or dictionary. I just not sure how to cycle through the text when the button is pushed. I can get the first and the last text. I just lost on the best process or logic to achieve this.
I have produced the application is tkinter, but was unable to achieve this functionality there either. I am using PyQt now because it has less limitations.
The code looks something like this:
import configparser
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import random
appGlobal = {'dialogCount':0}
class GameDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QGridLayout(self)
lblWBS = QLabel("lblWBS")
lblDialog = QLabel("lblDialog")
btnOK = QPushButton("OK")
layout.addWidget(btnOK, 5, 1)
lblDialog = QLabel()
layout.addWidget(lblWBS, 0, 1)
layout.addWidget(lblDialog, 1, 1)
def npcDialog():
lblDialog.setText(message[appGlobal['dialogCount']])
layout.removeWidget(lblDialog)
layout.addWidget(lblDialog, 1, 1)
appGlobal['dialogCount'] =+ 1
npcDialog()
btnOK.clicked.connect(npcDialog)
self.setWindowTitle("PALCDMS")
def kickoffMeeting():
kickoff_meeting = config['kickoffDialog']
global message
message = []
for name in kickoff_meeting:
string_value = config.get('kickoffDialog', name)
message.append(string_value)
app = QApplication(sys.argv)
dialog = GameDialog()
dialog.show()
app.exec_()
kickoffMeeting()
Solution
The simplest approach you can follow is to have simply a QPushButton, connect its click with a function where you do your behavior and that's all. I got some example as simple as I could, have a look on this and see if it makes sense.
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QPushButton
import random
class Label(QLabel):
def __init__(self):
super(Label, self).__init__()
self.letters = ['q','w','e','r','t','y']
self.h_layout = QHBoxLayout()
self.setLayout(self.h_layout)
self.label = QLabel('Random letters: _')
self.btn = QPushButton("Roll")
self.btn.clicked.connect(self.change_label)
self.h_layout.addWidget(self.label)
self.h_layout.addWidget(self.btn)
def change_label(self):
if len(self.letters)>0:
self.label.setText(self.letters.pop(0))
if __name__=="__main__":
app = QApplication(sys.argv)
main_label = Label()
main_label.show()
sys.exit(app.exec_())
I think your biggest issue is your indentation. Don't know how new you are with python but that code you posted seems to have a pretty weird indentation. I had some issues to understand your logic.
Answered By - yurisnm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.