Python Forum
help needed running a simple function in pyqt5 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: help needed running a simple function in pyqt5 (/thread-36225.html)

Pages: 1 2 3


RE: help needed running a simple function in pyqt5 - menator01 - Feb-16-2022

Have you looked at the last updated code I posted? https://python-forum.io/thread-36225-post-153444.html#pid153444


RE: help needed running a simple function in pyqt5 - menator01 - Feb-16-2022

change entry = QLineEdit(.................
to self.entry = QlineEdit(..................


RE: help needed running a simple function in pyqt5 - diodes - Feb-16-2022

(Feb-16-2022, 08:53 PM)menator01 Wrote: change entry = QLineEdit(.................
to self.entry = QlineEdit(..................

we did it, my friend. or should I say you did it:) it works perfectly. thanks for the help. would you know of a safe way to make these two small programs executable on both Linux and Windows? I tried one method and it gave me a virus not sure what was in the code after compiling? I ended up needing to re-install ubuntu to get it safe again.


RE: help needed running a simple function in pyqt5 - deanhystad - Feb-16-2022

Very unlikely you made a virus.

https://answers.microsoft.com/en-us/protect/forum/all/python-compiled-files-pyc-falsely-detected-as/7172e5b1-5c65-4d9c-811b-5ae08a4d52d2

You cannot make an executable that runs on windows an Linux. You would make an exe for windows and another for Linux.


RE: help needed running a simple function in pyqt5 - diodes - Feb-16-2022

(Feb-16-2022, 10:30 PM)deanhystad Wrote: Very unlikely you made a virus.

https://answers.microsoft.com/en-us/protect/forum/all/python-compiled-files-pyc-falsely-detected-as/7172e5b1-5c65-4d9c-811b-5ae08a4d52d2

You cannot make an executable that runs on windows an Linux. You would make an exe for windows and another for Linux.

I had figured the same thank you for that article. Would you happen to know of any resource that could guide me into making an exe and a Linux executable program out of these programs? Or maybe a thread that would help.


RE: help needed running a simple function in pyqt5 - deanhystad - Feb-17-2022

I think pyinstaller is the most common tool for this. Other choices I've heard about are cx freeze and auto py to exe. There are probably others.


RE: help needed running a simple function in pyqt5 - diodes - Feb-17-2022

Thanks for all the help on this little program. I am now trying to adapt it to run this roulette code for playing odd or even and red or black.

import random

print('Bill does Odd or Even')
print('and Red and Black')
x = (random.randint(0,1))
if x == 0: 
    print('odd')
if x == 1:
    print('even')       
y = (random.randint(2,3))
if y == 2:
    print('red')
if y == 3:
    print('black')
  
This is my GUI interface but not sure how to accomplished that basic code.

import random as rnd
import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QPushButton, QGridLayout, QLabel,
QLineEdit, QListWidget, QMessageBox)
 
 
class Window(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
 
        # Container
        container = QGridLayout()

        label = QLabel('Odd or Even')
        label2 = QLabel('Red or Black?')

        
        button = QPushButton('Spin')
        button2 = QPushButton('clear')
        button.clicked.connect(lambda: self.picker)
        button2.clicked.connect(self.clear)
        
        self.listbox = QListWidget()

        container.addWidget(label, 0, 0, 1, 1)
        container.addWidget(label2, 0, 1, 1, 1)
        container.addWidget(button, 1, 0, 1, 1)
        container.addWidget(button2, 1, 1, 1, 2)
        container.addWidget(self.listbox, 2, 0 ,1 ,2)
        
        widget = QWidget()
        widget.setLayout(container)
        self.setCentralWidget(widget)


    def clear(self):
        self.listbox.clear()

    def picker(self):
        x = (rnd.randint(0, 1))
        if x == 0:
            print('Odd')
        if x == 1:
            print('even')
        y = (rnd.randint(2,3))
        if y == 2:
            print('red')
        if y == 3:
            print('black') 
        nums = ', '
        self.listbox.addItem(nums)


def main():
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
 
main()
Is it possible to run that with like a while function. not sure how to get it to execute.


RE: help needed running a simple function in pyqt5 - menator01 - Feb-17-2022

You might want to look at this approach

import random as rnd

def odd_even():
    return rnd.choice(['odd', 'even'])

def red_black():
    return rnd.choice(('red', 'black'))

for i in range(5):
    print(odd_even())
print('\n')
for i in range(5):
    print(red_black())
Output:
even odd even odd even black red red black black



RE: help needed running a simple function in pyqt5 - diodes - Feb-17-2022

that is a good way to go, but I was wanting to get that into a GUI window, that I can later make executable. Not sure how to get the results to print in the listbox one after the other. And to have the clear button clear the list box when full.
here's the best I could come up with, though it does not print the results into the listbox
import random as rnd
import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QPushButton, QGridLayout, QLabel,
QLineEdit, QListWidget, QMessageBox)
 
 
class Window(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
 
        # Container
        container = QGridLayout()

        label = QLabel('Odd or Even?')
        label2 = QLabel('Red or Black?')

        
        button = QPushButton('Spin')
        button2 = QPushButton('clear')
        button.clicked.connect(lambda: self.printit)
        button2.clicked.connect(self.clear)
        
        self.listbox = QListWidget()

        container.addWidget(label, 0, 0, 1, 1)
        container.addWidget(label2, 0, 1, 1, 1)
        container.addWidget(button, 1, 0, 1, 1)
        container.addWidget(button2, 1, 1, 1, 2)
        container.addWidget(self.listbox, 2, 0 ,1 ,2)
        
        widget = QWidget()
        widget.setLayout(container)
        self.setCentralWidget(widget)


    def clear(self):
        self.listbox.clear()

    def odd_even():
        return rnd.choice(['odd', 'even'])
 
    def red_black():
        return rnd.choice(['red', 'black'])

    def printit(self):
        nums = ', '.join(red_black, odd_even)
        self.listbox.addItem(nums) 
    


def main():
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
 
main()
also, it only needs to pick one of each every time you click the button.


RE: help needed running a simple function in pyqt5 - menator01 - Feb-17-2022

import random as rnd

import sys
from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget, QPushButton, QGridLayout, QLabel,
QLineEdit, QListWidget, QListWidgetItem, QMessageBox)
from PyQt5.QtCore import Qt


class Window(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Container
        container = QGridLayout()

        label = QLabel('How many numbers?')
        label2 = QLabel('How many spots?')

        self.entry = QLineEdit()
        self.entry.setPlaceholderText('How many numbers?')
        self.entry.setStyleSheet('border: 1px solid black;')

        self.entry2 = QLineEdit()
        self.entry2.setPlaceholderText('How many spots?')
        self.entry2.setStyleSheet('border: 1px solid black;')

        button = QPushButton('Submit')
        button.clicked.connect(lambda: self.picker(self.entry, self.entry2))
        button2 = QPushButton('Clear')
        button2.clicked.connect(self.clear)

        spinbtn = QPushButton('Spin')
        spinbtn.clicked.connect(lambda: self.odd_even())

        self.listbox = QListWidget()

        container.addWidget(label, 0, 0, 1, 1)
        container.addWidget(self.entry, 0, 1, 1, 1)
        container.addWidget(label2, 1, 0, 1, 1)
        container.addWidget(self.entry2, 1, 1, 1, 1)
        container.addWidget(button, 2, 0, 1, 1)
        container.addWidget(button2, 2, 1, 1, 1)
        container.addWidget(self.listbox, 3, 0, 1, 2)
        container.addWidget(spinbtn, 4, 0, 1, 2)

        widget = QWidget()
        widget.setLayout(container)
        self.setCentralWidget(widget)

    def odd_even(self):
        color = rnd.choice(('red', 'black'))
        item = QListWidgetItem(f"{rnd.choice(('odd', 'even'))} {color}")
        if color == 'red':
            item.setForeground(Qt.red)
        self.listbox.addItem(item)


    def clear(self):
        self.listbox.clear()

    def picker(self, how_many, spots):
        messages = ['Spot\'s can not be less than number', 'Only numbers can be entered']
        error = 0
        try:
            spots = int(spots.text())
            how_many = int(how_many.text())
            if spots < how_many:
                text = messages[0]
                error += 1
        except ValueError:
            error += 1
            text = messages[1]

        if error > 0:
            msg = QMessageBox()
            msg.setText(text)
            msg.setIcon(QMessageBox.Warning)
            msg.setWindowTitle('Warning Error!')
            msg.setStandardButtons(QMessageBox.Ok)
            msg.exec_()
        else:
            picks = []
            while len(picks) < how_many:
                pick = rnd.randint(1, spots)
                if str(pick) not in picks:
                    picks.append(f'{pick}')
            nums = ', '.join(picks)
            self.listbox.addItem(nums)
        self.entry.clear()
        self.entry2.clear()


def main():
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

main()