Python Forum
help needed running a simple function in pyqt5
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help needed running a simple function in pyqt5
#11
Have you looked at the last updated code I posted? https://python-forum.io/thread-36225-pos...#pid153444
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#12
change entry = QLineEdit(.................
to self.entry = QlineEdit(..................
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#13
(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.
Reply
#14
Very unlikely you made a virus.

https://answers.microsoft.com/en-us/prot...e08a4d52d2

You cannot make an executable that runs on windows an Linux. You would make an exe for windows and another for Linux.
Reply
#15
(Feb-16-2022, 10:30 PM)deanhystad Wrote: Very unlikely you made a virus.

https://answers.microsoft.com/en-us/prot...e08a4d52d2

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.
Reply
#16
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.
Reply
#17
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.
Reply
#18
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#19
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.
Reply
#20
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()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  simple tkinter question function call not opening image gr3yali3n 5 3,432 Aug-02-2022, 09:13 PM
Last Post: woooee
  Tkinter won't run my simple function AthertonH 6 3,844 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,011 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  A Simple PyQt5 Class zoro 15 5,757 Mar-02-2020, 07:25 PM
Last Post: micseydel
  Refresh image in label after every 1s using simple function jenkins43 1 5,491 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,825 Apr-06-2019, 11:15 PM
Last Post: ZenWoR

Forum Jump:

User Panel Messages

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