How to combine two pieces of code?
-
Hey guys, so I am quite new to coding, so excuse me if this is a silly question, but I am trying to insert code for a scroll bar into code I have for an event form for an interactive calendar. Each set of code works by itself, but when I try to insert the scroll bar code into my event form file it simply doesn't show up. I've tried several combinations and locations, to no avail. I have the two files copied below. I am trying to get the scroll bar just below the self.note_edit block. Any insight would be incredibly appreciated.
# event_form.pyw #Import Statements import sys from datetime import datetime from pymongo import MongoClient from PyQt5.QtCore import QDate, Qt from PyQt5.QtGui import ( QPen, QFont, QColor, QBrush, QPainter, ) from PyQt5.QtWidgets import ( QLabel, QWidget, QLineEdit, QTextEdit, QCheckBox, QComboBox, QApplication, QPushButton, ) #Initizializes the Database. client = MongoClient("localhost", 27017) db = client["mydb"] coll = db["appointments"] #Initializes the GUI Class. class EventForm(QWidget): def __init__(self): super(EventForm, self).__init__() self.initUI() def initUI(self): self.left = 685 self.top = 0 self.width = 685 self.height = 730 self.setWindowFlags(Qt.FramelessWindowHint) self.setGeometry(self.left,self.top,self.width,self.height) self.setAutoFillBackground(True) p = self.palette() p.setColor(self.backgroundRole(), Qt.darkGray) self.setPalette(p) qdim = "QPushButton {color: rgba(0, 59, 42, 1); font: bold; background-color: rgba(29, 29, 29, 150); }" segoe_9 = QFont( 'Segoe UI', 9 ) segoe_20 = QFont( "Segoe UI", 20 ) canda_10 = QFont( "Candalara", 10 ) canda_11 = QFont( "Candalara", 11 ) canda_12 = QFont( "Candalara", 12 ) canda_16 = QFont( "Candalara", 16) #Combo Box for input times (half-hour increments). times = [ 'All Day','12:00 AM', '12:05 AM', '12:10 AM', '12:15 AM', '12:20 AM', '12:25 AM', '12:30 AM', '12:35 AM', '12:40 AM', '12:45 AM', '12:50 AM', '12:55 AM','1:00 AM', '1:05 AM', '1:10 AM', '1:15 AM', '1:20 AM', '1:25 AM', '1:30 AM', '1:35 AM', '1:40 AM', '1:45 AM', '1:50 AM', '1:55 AM','2:00 AM', '2:05 AM', '2:10 AM', '2:15 AM', '2:20 AM', '2:25 AM', '2:30 AM', '2:35 AM', '2:40 AM', '2:45 AM', '2:50 AM', '2:55 AM','3:00 AM', '3:05 AM', '3:10 AM', '3:15 AM', '3:20 AM', '3:25 AM', '3:30 AM', '3:35 AM', '3:40 AM', '3:45 AM', '3:50 AM', '3:55 AM','4:00 AM', '4:05 AM', '4:10 AM', '4:15 AM', '4:20 AM', '4:25 AM', '4:30 AM', '4:35 AM', '4:40 AM', '4:45 AM', '4:50 AM', '4:55 AM','5:00 AM', '5:05 AM', '5:10 AM', '5:15 AM', '5:20 AM', '5:25 AM', '5:30 AM', '5:35 AM', '5:40 AM', '5:45 AM', '5:50 AM', '5:55 AM', '6:00 AM','6:05 AM', '6:10 AM', '6:15 AM', '6:20 AM', '6:25 AM', '6:30 AM', '6:35 AM', '6:40 AM', '6:45 AM', '6:50 AM', '6:55 AM', '7:00 AM','7:05 AM', '7:10 AM', '7:15 AM', '7:20 AM', '7:25 AM', '7:30 AM', '7:35 AM', '7:40 AM', '7:45 AM', '7:50 AM', '7:55 AM', '8:00 AM','8:05 AM', '8:10 AM', '8:15 AM', '8:20 AM', '8:25 AM', '8:30 AM', '8:35 AM', '8:40 AM', '8:45 AM', '8:50 AM', '8:55 AM', '9:00 AM','9:05 AM', '9:10 AM', '9:15 AM', '9:20 AM', '9:25 AM', '9:30 AM', '9:35 AM', '9:40 AM', '9:45 AM', '9:50 AM', '9:55 AM', '10:00 AM','10:05 AM', '10:10 AM', '10:15 AM', '10:20 AM', '10:25 AM', '10:30 AM', '10:35 AM', '10:40 AM', '10:45 AM', '10:50 AM', '10:55 AM', '11:00 AM','11:05 AM', '11:10 AM', '11:15 AM', '11:20 AM', '11:25 AM', '11:30 AM', '11:35 AM', '11:40 AM', '11:45 AM', '11:50 AM', '11:55 AM', '12:00 PM','12:05 PM', '12:10 PM', '12:15 PM', '12:20 PM', '12:25 PM', '12:30 PM', '12:35 PM', '12:40 PM', '12:45 PM', '12:50 PM', '12:55 PM', '1:00 PM','1:05 PM', '1:10 PM', '1:15 PM', '1:20 PM', '1:25 PM', '1:30 PM', '1:35 PM', '1:40 PM', '1:45 PM', '1:50 PM', '1:55 PM', '2:00 PM','2:05 PM', '2:10 PM', '2:15 PM', '2:20 PM', '2:25 PM', '2:30 PM', '2:35 PM', '2:40 PM', '2:45 PM', '2:50 PM', '2:55 PM', '3:00 PM','3:05 PM', '3:10 PM', '3:15 PM', '3:20 PM', '3:25 PM', '3:30 PM', '3:35 PM', '3:40 PM', '3:45 PM', '3:50 PM', '3:55 PM', '4:00 PM','4:05 PM', '4:10 PM', '4:15 PM', '4:20 PM', '4:25 PM', '4:30 PM', '4:35 PM', '4:40 PM', '4:45 PM', '4:50 PM', '4:55 PM', '5:00 PM','5:05 PM', '5:10 PM', '5:15 PM', '5:20 PM', '5:25 PM', '5:30 PM', '5:35 PM', '5:40 PM', '5:45 PM', '5:50 PM', '5:55 PM', '6:00 PM','6:05 PM', '6:10 PM', '6:15 PM', '6:20 PM', '6:25 PM', '6:30 PM', '6:35 PM', '6:40 PM', '6:45 PM', '6:50 PM', '6:55 PM', '7:00 PM','7:05 PM', '7:10 PM', '7:15 PM', '7:20 PM', '7:25 PM', '7:30 PM', '7:35 PM', '7:40 PM', '7:45 PM', '7:50 PM', '7:55 PM', '8:00 PM','8:05 PM', '8:10 PM', '8:15 PM', '8:20 PM', '8:25 PM', '8:30 PM', '8:35 PM', '8:40 PM', '8:45 PM', '8:50 PM', '8:55 PM', '9:00 PM','9:05 PM', '9:10 PM', '9:15 PM', '9:20 PM', '9:25 PM', '9:30 PM', '9:35 PM', '9:40 PM', '9:45 PM', '9:50 PM', '9:55 PM', '10:00 PM','10:05 PM', '10:10 PM', '10:15 PM', '10:20 PM', '10:25 PM', '10:30 PM', '10:35 PM', '10:40 PM', '10:45 PM', '10:50 PM', '10:55 PM', '10:00 PM','11:05 PM', '11:10 PM', '11:15 PM', '11:20 PM', '11:25 PM', '11:30 PM', '11:35 PM', '11:40 PM', '11:45 PM', '11:50 PM', '11:55 PM', ] #Reads the date string from the text file, formats for display on the form and entry to the database. with open('string_date.txt', "r") as file: data = file.read() num = datetime.strptime(data, '%Y-%m-%d') day = str(num.strftime( '%A' )) date = str(num.strftime('%m-%d-%Y')) #Labels the day name and date that was clicked on. self.date_data = QLabel(self) self.date_data.setGeometry( 143, 15, 400, 40 ) self.date_data.setStyleSheet( "QLabel {color: rgba(0, 59, 42, 1); font: bold; background-color: gray; border: none}" ) self.date_data.setFont( segoe_20 ) self.date_data.setAlignment(Qt.AlignCenter) self.date_data.setText(day + " " + date) #Displays the "Time", "Place" and "Note" text. self.time_label = QLabel(self) self.time_label.setGeometry(80, 95, 80, 50) self.time_label.setStyleSheet( "QLabel {color: rgba(0, 59, 42, 1); font: bold; background-color: gray;}" ) self.time_label.setFont(canda_16) self.time_label.setText("Time:") self.place_label = QLabel(self) self.place_label.setGeometry(75, 170, 80, 35) self.place_label.setStyleSheet( "QLabel {color: rgba(0, 59, 42, 1); font: bold; background-color: gray;}" ) self.place_label.setFont(canda_16) self.place_label.setText("Place:") self.note_label = QLabel(self) self.note_label.setGeometry( 73, 235, 80, 40 ) self.note_label.setStyleSheet( "QLabel {color: rgba(0, 59, 42, 1); font: bold; background-color: gray;}" ) self.note_label.setFont( canda_16 ) self.note_label.setText( "Notes:" ) self.stress_label = QLabel(self) self.stress_label.setGeometry(75, 340, 100, 60) self.stress_label.setStyleSheet("QLabel {color: rgba(0, 59, 42, 1); font: bold; background-color: gray;}") self.stress_label.setFont(canda_16) self.stress_label.setText("Stress\n Level :") #Combo box of time choices. self.time_cbox = QComboBox( self ) self.time_cbox.setGeometry( 160, 95, 125, 50 ) self.time_cbox.setStyleSheet( "QComboBox {color: rgba(0, 59, 42, 1); font: bold; background-color: rgba(0, 4, 0, 0.22);}" ) self.time_cbox.setFont( canda_12 ) for time in times: self.time_cbox.addItem(time) self.time_cbox.currentIndexChanged.connect(self.selectionchange) #Creates a place to type in appointment location or other information. self.place_edit = QLineEdit( self ) self.place_edit.setGeometry(160, 165, 400, 50 ) self.place_edit.setStyleSheet( "QLineEdit {color: rgba(0, 59, 42, 1); font: bold; background-color: rgba(0, 4, 0, 0.22); border: none;}" ) self.place_edit.setFont( canda_12 ) self.note_edit = QTextEdit(self) self.note_edit.setGeometry( 160, 235, 400, 80 ) self.note_edit.setStyleSheet( "QTextEdit {color: rgba(0, 59, 42, 1); background-color: rgba(0, 4, 0, 0.22); border: none;}" ) self.note_edit.setFont( canda_12 ) # Creates the three buttons: "Save", "Exit", "Save and Exit" self.save_button = QPushButton(self) self.save_button.setStyleSheet(qdim) self.save_button.setFont(canda_16) self.save_button.setText("SAVE") self.save_button.setGeometry(230, 485, 100, 50) self.save_button.clicked.connect(self.save) self.exit_button = QPushButton(self) self.exit_button.setStyleSheet(qdim) self.exit_button.setFont(canda_16) self.exit_button.setText("EXIT") self.exit_button.setGeometry(345, 485, 100, 50) self.exit_button.clicked.connect(self.exitt) self.save_exit_button = QPushButton(self) self.save_exit_button.setStyleSheet(qdim) self.save_exit_button.setFont(canda_16) self.save_exit_button.setText("SAVE AND EXIT") self.save_exit_button.setGeometry(230, 550, 215, 75) self.save_exit_button.clicked.connect(self.save_exit) #Sets focus on the next text entry box. def selectionchange(self): self.place_edit.setFocus() #Draws the two separator lines. def paintEvent(self, e): self.painter = QPainter( self ) self.painter.begin( self ) self.painter.setPen(QColor(75, 75, 75)) self.painter.drawLine(45, 65, 640, 65) self.painter.drawLine(45, 450, 640, 450 ) self.painter.end() #Repeats the "save()" and "save_exit()" functions. The "-10" removes the day name from the string. def repeat(self): date = self.date_data.text()[-10:] time = self.time_cbox.currentText() place = self.place_edit.text() note = self.note_edit.toPlainText() coll.insert_one({'date': date, "time": time, 'place': place, 'note': note}) #Three button functions: def save(self): data = self.repeat() self.place_edit.clear() self.note_edit.clear() def save_exit(self): data = self.repeat() self.exitt() def exitt(self): self.close() if __name__ == "__main__": app = QApplication(sys.argv) ex = EventForm() ex.show() sys.exit(app.exec_())
# Scroll Bar Code #importing libraries from PyQt5.QtWidgets import * from PyQt5 import QtCore, QtGui from PyQt5.QtGui import * from PyQt5.QtCore import * import sys class Window(QMainWindow): def __init__(self): super().__init__() # setting title self.setWindowTitle("Python ") # setting geometry self.setGeometry(100, 100, 500, 400) # calling method self.UiComponents() # showing all the widgets self.show() # method for components def UiComponents(self): scroll = QScrollBar(self) # setting geometry of the scroll bar scroll.setGeometry(100, 50, 200, 30) # making its background color to green scroll.setStyleSheet("background : rgba(0, 59, 42, 1);") # setting single step scroll.setSingleStep(25) # setting orientation scroll.setOrientation(Qt.Horizontal) # creating a label label = QLabel("Stress Level", self) # setting geometry to the label label.setGeometry(200, 100, 300, 80) # making label multi line label.setWordWrap(True) # getting value changed signal scroll.valueChanged.connect(lambda: do_action()) # method called when signal is emitted def do_action(): # setting text to the label label.setText("Stress Level : " + str(scroll.value())) #create pyqt5 app App = QApplication(sys.argv) #create the instance of our Window window = Window() #start the app sys.exit(App.exec())
-
*EDIT: Not sure why the formatting is so weird with the code, if I need to change it let me know.
-
@JMad0436
Yes, please place code blocks inside forum's Code tags ---</>
on toolbar here --- or line with```
(3 backticks) above and below block.Hi,
Your scroll and label variable are local to the function hence they will be destroyed at the end of it and never show.
On a side note, don't call show in the
__init__
method. It's not the role of the widget to force itself to show. That's the role of the class or method that will manage it to do that. -
Hi,
Your scroll and label variable are local to the function hence they will be destroyed at the end of it and never show.
On a side note, don't call show in the
__init__
method. It's not the role of the widget to force itself to show. That's the role of the class or method that will manage it to do that.@SGaist So I should disregard the block initializing a window, and set those variables by themselves within the class already written in the other file? I believe what you pointed out is why it's not showing up I'm just not positive how/where I should write those variables in order for them to show up.
-
@SGaist So I should disregard the block initializing a window, and set those variables by themselves within the class already written in the other file? I believe what you pointed out is why it's not showing up I'm just not positive how/where I should write those variables in order for them to show up.
@JMad0436 Python basics.
def UiComponents(self): self.scroll = QScrollBar(self) # setting geometry of the scroll bar self.scroll.setGeometry(100, 50, 200, 30) # making its background color to green self. scroll.setStyleSheet("background : rgba(0, 59, 42, 1);") # setting single step self.scroll.setSingleStep(25) # setting orientation self.scroll.setOrientation(Qt.Horizontal) # creating a label self.label = QLabel("Stress Level", self) # setting geometry to the label self.label.setGeometry(200, 100, 300, 80) # making label multi line self.label.setWordWrap(True) # getting value changed signal self.scroll.valueChanged.connect(lambda: do_action()) # method called when signal is emitted def do_action(): # setting text to the label self.label.setText("Stress Level : " + str(scroll.value()))