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
#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


Messages In This Thread
RE: help needed running a simple function in pyqt5 - by diodes - Feb-17-2022, 06:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How To Make A PyQt5 Progress Bar Run While Executing A Function rcwildabeast 1 538 Apr-26-2024, 02:18 AM
Last Post: menator01
  simple tkinter question function call not opening image gr3yali3n 5 3,720 Aug-02-2022, 09:13 PM
Last Post: woooee
  Tkinter won't run my simple function AthertonH 6 4,168 May-03-2022, 02:33 PM
Last Post: deanhystad
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,202 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  A Simple PyQt5 Class zoro 15 5,989 Mar-02-2020, 07:25 PM
Last Post: micseydel
  Refresh image in label after every 1s using simple function jenkins43 1 5,556 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,928 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