Python Forum
Trying to help stressed out daughter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to help stressed out daughter
#1
Hi,

Let me first say I know nothing about python though I am an IT professional. My daughter is trying to write a Python program but it's failing. She's got a GUI that pops up dutifully when the code is run and she wants another little dialog box to pop up when a button is pressed in the GUI.... Cool. But when she adds the specific function that should be run when the button is pressed, it seems to screw up the whole program preventing the GUI from running at all! So I'm thinking that somehow the function code she adds needs to kind of be isolated from the main program and only run when the specific GUI button is pressed but she says she has done that by writing def before it....

Like I said I'm clueless and I'm just trying to help her. Any pointers would be appreciated.

TIA

Olly
Reply
#2
It's really difficult to tell without seeing any code.
Post the code you have in python tags, full traceback if you get any - in error tags.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hey thanks for replying Buran. The code is below and I put a bunch of $$$$$$ before the bit she says is a function coz I can't see how to colour the text red! It's near the end. Thanks again

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QFrame, QApplication, QWidget, QPushButton, QColorDialog, QSlider
from PyQt5.QtGui import QIcon, QColor
from PyQt5.QtCore import Qt
 

class StartScreen():
def GUI(self, MainWindow):
 
 
MainWindow.setFixedSize(395, 290)
MainWindow.setWindowTitle("Start Screen")
 
self.centralwidget = QtWidgets.QWidget(MainWindow)

#----------------------------Layouts----------------------------

self.gridL = QtWidgets.QGridLayout(self.centralwidget)
 
spacerItem = QtWidgets.QSpacerItem(374, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridL.addItem(spacerItem, 6, 0, 1, 1)

self.formL = QtWidgets.QFormLayout()

#----------------------------Labels----------------------------
 
self.label_1 = QtWidgets.QLabel(self.centralwidget)
self.label_1.setText("No. of Pendulums")
self.formL.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_1)
 
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setText("Decay Factor")  
self.formL.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)

self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setText("Max Frequency")
self.formL.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_3)

self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setText("Frequency Spread")
self.formL.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.label_4)

self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setText("Speed")
self.formL.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.label_5)

#----------------------------Sliders----------------------------

self.noPendSlider = QtWidgets.QSlider(self.centralwidget)
self.noPendSlider.setMinimumSize(QtCore.QSize(281, 0))
self.noPendSlider.setOrientation(QtCore.Qt.Horizontal)
self.formL.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.noPendSlider)
 
self.decaySlider = QtWidgets.QSlider(self.centralwidget)
self.decaySlider.setOrientation(QtCore.Qt.Horizontal)
self.formL.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.decaySlider)
 
self.maxFreqSlider = QtWidgets.QSlider(self.centralwidget)
self.maxFreqSlider.setOrientation(QtCore.Qt.Horizontal)
self.formL.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.maxFreqSlider)
 
self.freqSpSlider = QtWidgets.QSlider(self.centralwidget)
self.freqSpSlider.setOrientation(QtCore.Qt.Horizontal)
self.formL.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.freqSpSlider)
 
self.speedSlider = QtWidgets.QSlider(self.centralwidget)
self.speedSlider.setOrientation(QtCore.Qt.Horizontal)
self.formL.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.speedSlider)
 
self.gridL.addLayout(self.formL, 0, 0, 1, 1)
self.vertL = QtWidgets.QVBoxLayout()
 
self.startBtn = QtWidgets.QPushButton(self.centralwidget)
self.startBtn.setText("Start")
self.startBtn.setToolTip("Start the simulation")
self.vertL.addWidget(self.startBtn)
 
self.gridL.addLayout(self.vertL, 7, 0, 1, 1)
 
spacerItem1 = QtWidgets.QSpacerItem(455, 17, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
 
self.gridL.addItem(spacerItem1, 2, 0, 1, 1)
 
self.vertL_3 = QtWidgets.QVBoxLayout()
self.hozL_2 = QtWidgets.QHBoxLayout()

#----------------------------Colour-Dialog----------------------------
 
self.colourLineBtn = QtWidgets.QPushButton(self.centralwidget)
self.colourLineBtn.setText("Line Colour")
self.colourLineBtn.setToolTip("Change the line colour")
self.hozL_2.addWidget(self.colourLineBtn)
#self.colourLineBtn.clicked.connect(self.showDialog)

self.colourBgBtn = QtWidgets.QPushButton(self.centralwidget)
self.colourBgBtn.setText("Background colour")
self.colourBgBtn.setToolTip("Change the background colour")
# self.colourBgBtn.clicked.connect(self.showDialog)
self.hozL_2.addWidget(self.colourBgBtn)
 
 
#col = QColor(0,0,0)
 
#self.frm = QFrame(self.centralwidget)
#self.frm.setStyleSheet("QWidget { background-color: %s }" 
# % col.name())

$$$$$$$$$$
# def showDialog(self):
 
# col = QColorDialog.getColor()
 
# if col.isValid():
# self.frm.setStyleSheet("QWidget { background-color: %s }"
# % col.name())


 
self.vertL_3.addLayout(self.hozL_2)
self.formL_4 = QtWidgets.QFormLayout()
 
self.lineWidthSlider = QtWidgets.QSlider(self.centralwidget)
self.lineWidthSlider.setOrientation(QtCore.Qt.Horizontal)
self.formL_4.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.lineWidthSlider)
 
self.lineWidthLbl = QtWidgets.QLabel(self.centralwidget)
self.lineWidthLbl.setText("Line Width")
self.lineWidthLbl.setToolTip("Change the line width")
self.formL_4.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.lineWidthLbl)
 
self.vertL_3.addLayout(self.formL_4)
self.gridL.addLayout(self.vertL_3, 3, 0, 1, 1)
 
MainWindow.setCentralWidget(self.centralwidget)

#----------------------------Execute----------------------------
 
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = StartScreen()
ui.GUI(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Reply
#4
Please, repost with indentation preserved. Indentation matters in python.
Also, I would recommend to uncomment the code that gives you trouble. whoever try to help will want to reproduce the error. If you get any traceback - post it in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
If she can have her edit it in VS code (it's free) then she can format it with the indentation with the format command.
Gary
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help with Dice Game for daughter in school OptX 2 1,923 Feb-12-2021, 08:43 PM
Last Post: BashBedlam
  Im so stressed because I cant figure out what I'm doing wrong. BursteXO 2 3,012 Jan-29-2018, 01:16 AM
Last Post: BursteXO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020