Не могу заствить работать label

При нажатии pushButton выходит только часть текста, при закрытии остальная часть сообщения, также не выводится сообщение в строку label. Как исправить ?

import sys
# coding=utf8
import random
from PyQt5 import QtCore, QtGui, QtWidgets
from ma import Ui_Dialog
app = QtWidgets.QApplication(sys.argv)
#
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
#
def get_random ():
    ui.pushButton.clicked.connect( get_random )
rand = print("число",random.randint(1, 15))
list = [500, 1000, 1500, 2000, 2500, 3000, 3500]
print("число",random.choice(list))
    
ui.label.setText(str(rand))```





Ответы (1 шт):

Автор решения: S. Nick

Вы должны приводить минимально-воспроизводимый пример !

Вы не в том месте делаете привязку ui.pushButton.clicked.connect( get_random ), функция get_random никогда не вызывается и т.д. ...

import sys
import random
from PyQt5 import QtCore, QtGui, QtWidgets
#from ma import Ui_Dialog


app = QtWidgets.QApplication(sys.argv)

def get_random():                                               # !!!
    rand = f"число1 {random.randint(1, 15)}"
    text = f"число2 {random.choice(list)}"
    label.setText(f"{rand} \n{text}")
    label.adjustSize()  

Dialog = QtWidgets.QDialog()

#ui = Ui_Dialog()
#ui.setupUi(Dialog)

pushButton = QtWidgets.QPushButton("Click me", Dialog)
pushButton.clicked.connect(get_random)                          # <--- !!!
pushButton.move(20, 20)
label = QtWidgets.QLabel(Dialog)
label.move(120, 120)
Dialog.show()


#def get_random ():
#    ui.pushButton.clicked.connect( get_random )
#    ui.pushButton.clicked.connect( get_random )
    
#rand = print("число1", random.randint(1, 15))                  # результат print - None
rand = f"число1: {random.randint(1, 15)}"
print(rand)

list = [500, 1000, 1500, 2000, 2500, 3000, 3500]
print(f"число2: {random.choice(list)}")
    
#ui.label.setText(str(rand))
#label.setText(str(rand))

sys.exit(app.exec_())

введите сюда описание изображения

→ Ссылка