Python Forum
GUI Problem / call another function / fill QListwidget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI Problem / call another function / fill QListwidget
#1
I am a hobby programmer and would like to write a program that supports, among other things, the scanning of double-sided paper with a one-sided feeder.

I've made good progress with the content in vb.net...

However, I would like to deepen my python knowledge and have therefore decided to write the program in python.
For this I will translate my vb functionalities into the python language...

At the moment I'm in the process of creating the GUI and linking it to functions.

The following problems occur, which I could not solve with googling:

1) When I click on the "adjustments" button, the adjustments window opens.
If I then click on the "add profile" button, the "adjustments" window appears.
The same applies to the "add profile" button when I click on the "adjustments" button after the "add profile" button.

2) in the "LineEditWindow" class in the "save_profile_in_file" function:
I would like to call the "add_profile" function of the "MainWindow" class from this function and pass the "profile_name" variable. This should add the item "profile_name" to the QListwidget "listbox_profile".

I would be very happy about constructive help on these two problems!

Thank you very much for the support!!

import sys
from pathlib import Path

from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import (
    QApplication,
    QLabel,
    QMainWindow,
    QPushButton,
    QHBoxLayout,
    QVBoxLayout,
    QWidget,
    QGroupBox,
    QListWidget,
    QCheckBox,
    QRadioButton,
    QLineEdit,
    QFileDialog,
    QMessageBox,
)


class LineEditWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("abc")
        self.resize(300, 50)
        self.profile_name = ""
        self.i = 0
        self.layout = QHBoxLayout()
        self.lineedit = QLineEdit()
        self.lineedit.setFont(QFont('Times', 12))
        self.button_enter = QPushButton("save profile")
        self.button_enter.setFont(QFont('Times', 12))
        for z in [self.lineedit, self.button_enter]:
            self.layout.addWidget(z)
        self.layout2 = QVBoxLayout()
        self.label = QLabel("Please enter a profile's name")
        self.label.setFont(QFont('Times', 12))
        self.layout2.addWidget(self.label)
        self.layout2.addLayout(self.layout)
        self.setLayout(self.layout2)
        self.button_enter.clicked.connect(self.save_profile_in_file)

    def save_profile_in_file(self):
        profile_name = self.lineedit.text()
        print(profile_name)
        f = open("profiles.txt", "a")
        f.write(str(profile_name) + "\n")
        f.close
        self.lineedit.setText("")
        # # add to listbox
        main = MainWindow()
        main.add_profile(profile_name)


class AdjustmentsWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("abc")
        self.resize(300, 150)
        self.label = QLabel("adjustments")
        self.label.setFont(QFont('Times', 12))
        self.button_files_to_convert = QPushButton("files to convert path")
        self.button_files_to_convert.setFont(QFont('Times', 12))
        self.button_created_files = QPushButton("created files path")
        self.button_created_files.setFont(QFont('Times', 12))
        self.button_donate = QPushButton("donate")
        self.button_donate.setFont(QFont('Times', 12))
        self.layout = QVBoxLayout()
        for k in [self.label, self.button_files_to_convert, self.button_created_files, self.button_donate]:
            self.layout.addWidget(k)
        self.setLayout(self.layout)
        self.button_created_files.clicked.connect(self.button_created_files_clicked)
        self.button_files_to_convert.clicked.connect(self.button_files_to_convert_clicked)

    def button_created_files_clicked(self):
        path_created_files = str(QFileDialog().getExistingDirectory(self, 'Select path for created files'))
        f = open("pcf.txt", "w")
        f.write(self.path_created_files)
        f.close()

    def button_files_to_convert_clicked(self):
        path_files_to_convert = str(QFileDialog().getExistingDirectory(self, 'Select path for files to convert'))
        f = open("pftc.txt", "w")
        f.write(self.path_files_to_convert)
        f.close()


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        # No additional window yet
        self.w = None
        super(MainWindow, self).__init__(parent)
        self.profile_name = ""
        self.setWindowTitle("abc")
        self.resize(400, 600)
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        # center MainWindow to screen
        qr = self.frameGeometry()
        cp = self.screen().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())
        # --------------------- groupbox_have ---------------------
        self.groupbox_have = QGroupBox("what do you have?")
        self.groupbox_have.setFont(QFont('Times', 12))
        self.groupbox_have.font().setBold(True)
        self.groupbox_have_layout = QVBoxLayout()
        self.listbox_medium = QListWidget()
        self.listbox_medium.setFont(QFont('Times', 12))
        self.listbox_medium2 = QListWidget()
        self.listbox_medium2.setFont(QFont('Times', 12))
        self.checkbox = QCheckBox("stack contains patchTpages")
        self.checkbox.setFont(QFont('Times', 12))
        for x in [self.listbox_medium, self.listbox_medium2, self.checkbox]:
            self.groupbox_have_layout.addWidget(x)
        self.groupbox_have.setLayout(self.groupbox_have_layout)
        # --------------------- groupbox_receive ---------------------
        self.groupbox_receive = QGroupBox("what would you like to receive?")
        self.groupbox_receive.setFont(QFont('Times', 12))
        self.groupbox_receive.font().setBold(True)
        self.groupbox_receive_layout = QVBoxLayout()
        self.listbox_target = QListWidget()
        self.listbox_target.setFont(QFont('Times', 12))
        self.groupbox_move = QGroupBox("move file(s) to")
        self.groupbox_move.setFont(QFont('Times', 12))
        self.groupbox_move.font().setBold(True)
        self.checkbox_move_to = QCheckBox("move file(s) to desired folder(s)")
        self.checkbox_move_to.setFont(QFont('Times', 12))
        self.groupbox_move_layout = QVBoxLayout()
        self.groupbox_move_layout.addWidget(self.checkbox_move_to)
        self.groupbox_move.setLayout(self.groupbox_move_layout)
        self.groupbox_receive_layout = QVBoxLayout()
        for y in [self.listbox_target, self.groupbox_move]:
            self.groupbox_receive_layout.addWidget(y)
        self.groupbox_receive.setLayout(self.groupbox_receive_layout)
        # --------------------- groupbox_profile ---------------------
        self.groupbox_profile = QGroupBox("which profile should be used?")
        self.groupbox_profile.setFont(QFont('Times', 12))
        self.groupbox_profile.font().setBold(True)
        self.listbox_profile = QListWidget()
        self.listbox_profile.setFont(QFont('Times', 12))
        self.button_add_profile = QPushButton("add profile")
        self.button_add_profile.setFont(QFont('Times', 12))
        self.button_clear_profile = QPushButton("clear profile")
        self.button_clear_profile.setFont(QFont('Times', 12))
        self.groupbox_profile_intern_layout = QHBoxLayout()
        for x in [self.button_add_profile, self.button_clear_profile]:
            self.groupbox_profile_intern_layout.addWidget(x)
        self.groupbox_profile_layout = QVBoxLayout()
        self.groupbox_profile_layout.addWidget(self.listbox_profile)
        self.groupbox_profile_layout.addLayout(self.groupbox_profile_intern_layout)
        self.groupbox_profile.setLayout(self.groupbox_profile_layout)
        # --------------------------------------------------------------
        self.button_apply_selection = QPushButton("apply selection")
        self.button_apply_selection.setFont(QFont('Times', 12))
        self.button_adjustments = QPushButton("adjustments")
        self.button_adjustments.setFont(QFont('Times', 12))
        self.label = QLabel("abc")
        self.label.setFont(QFont('Times', 12))

        self.layout_base = QVBoxLayout()
        for c in [self.label, self.groupbox_have, self.groupbox_receive, self.groupbox_profile, self.button_apply_selection, self.button_adjustments]:
            self.layout_base.addWidget(c)
        self.central_widget.setLayout(self.layout_base)

        # clear listbox
        self.listbox_medium.clear()
        self.listbox_medium2.clear()
        self.listbox_target.clear()
        # fill listbox
        self.listbox_medium.insertItem(0, "paper")
        self.listbox_medium.insertItem(1, "file(s)")
        self.listbox_medium2.insertItem(0, "-")
        self.listbox_target.insertItem(0, "-")

        self.button_add_profile.clicked.connect(self.button_add_profile_clicked)
        self.button_clear_profile.clicked.connect(self.button_clear_profile_clicked)
        self.button_apply_selection.clicked.connect(self.button_apply_selection_clicked)
        self.button_adjustments.clicked.connect(self.button_adjustments_clicked)

        self.path_created_files = open("pcf.txt", "r").read()
        if self.path_created_files == "":
            QMessageBox.about(self, "Go to adjustments", "Please adjust created files path")

        self.path_files_to_convert = open("pftc.txt", "r").read()
        if self.path_files_to_convert == "":
            QMessageBox.about(self, "Go to adjustments", "Please adjust files to convert path")

    def button_add_profile_clicked(self):
        if self.w is None:
            self.w = LineEditWindow()
        self.w.show()

    def add_profile(self, profile_name):
        i = self.listbox_profile.count()
        self.listbox_profile.insertItem(i + 1, str(profile_name))

    def button_clear_profile_clicked(self):
        f = open("profiles.txt", "w")
        f.write("")
        f.close()

    def button_adjustments_clicked(self):
        if self.w is None:
            self.w = AdjustmentsWindow()
        self.w.show()

    def button_apply_selection_clicked(self):
        current_index = self.listbox_medium.currentRow()
        if current_index == 0:
            print("paper")
        if current_index == 1:
            print("files")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyleSheet(Path("stylesheet.qss").read_text())
    win = MainWindow()
    win.show()
    app.exec()
Reply


Messages In This Thread
GUI Problem / call another function / fill QListwidget - by flash77 - Jul-29-2023, 04:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get the color name of qlistwidget? flash77 5 1,441 Aug-13-2023, 07:01 PM
Last Post: flash77
  QListWidget, no item was selected flash77 4 1,147 Aug-02-2023, 09:31 AM
Last Post: Axel_Erfurt
  [PyQt] populate a QListWidget devilonline 1 1,722 Apr-10-2023, 02:52 AM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,709 Aug-02-2022, 09:13 PM
Last Post: woooee
  [PyQt] Call a function in FormA from FormB panoss 3 2,015 Jan-30-2022, 07:45 PM
Last Post: panoss
Question [PyQt] CSS Styling for a QLabel inside a QListWidget Alfalfa 2 5,285 Nov-30-2020, 02:59 AM
Last Post: Alfalfa
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 4,616 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  [PyQt] call a function with parametrs from another class atlass218 3 4,926 Feb-29-2020, 11:00 AM
Last Post: atlass218
  [Tkinter] Call a function when switching layouts 4096 0 3,626 Sep-22-2019, 07:39 PM
Last Post: 4096
  [PyQt] call a function in another class darktitan 6 14,376 Jun-22-2019, 03:33 AM
Last Post: darktitan

Forum Jump:

User Panel Messages

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