Bottom Page

Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Code not working
#1
import random
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):

        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(550, 650)

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.CharacterRace  = QtWidgets.QListWidget(self.centralwidget)
        self.CharacterRace.setGeometry(QtCore.QRect(10, 10, 125, 25))
        self.CharacterRace.setObjectName("CharacterRace")

        self.GenerateRace = QtWidgets.QPushButton(self.centralwidget)
        self.GenerateRace.setGeometry(QtCore.QRect(10, 45, 125, 25))
        self.GenerateRace.setObjectName("GenerateRaceBtn")

        self.CharacterClass = QtWidgets.QListWidget(self.centralwidget)
        self.CharacterClass.setGeometry(QtCore.QRect(145, 10, 125, 25))
        self.CharacterClass.setObjectName("CharacterClass")

        self.GenerateClass = QtWidgets.QPushButton(self.centralwidget)
        self.GenerateClass.setGeometry(QtCore.QRect(145, 45, 125, 25))
        self.GenerateClass.setObjectName("GenerateClassBtn")

        self.CharacterSubrace = QtWidgets.QListWidget(self.centralwidget)
        self.CharacterSubrace.setGeometry(QtCore.QRect(280, 10, 125, 25))
        self.CharacterSubrace.setObjectName("CharacterSubrace")

        self.GenerateSubrace = QtWidgets.QPushButton(self.centralwidget)
        self.GenerateSubrace.setGeometry(QtCore.QRect(280, 45, 125, 25))
        self.GenerateSubrace.setObjectName("GenerateSubraceBtn")

        self.CharacterSubclass = QtWidgets.QListWidget(self.centralwidget)
        self.CharacterSubclass.setGeometry(QtCore.QRect(415, 10, 125, 25))
        self.CharacterSubclass.setObjectName("CharacterSubclass")

        self.GenerateSubclass = QtWidgets.QPushButton(self.centralwidget)
        self.GenerateSubclass.setGeometry(QtCore.QRect(415, 45, 125, 25))
        self.GenerateSubclass.setObjectName("GenerateSubclass")

        self.GenerateCharacter = QtWidgets.QPushButton(self.centralwidget)
        self.GenerateCharacter.setGeometry(QtCore.QRect(10, 80, 530, 25))
        self.GenerateCharacter.setObjectName("GenerateCharacter")

        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.GenerateRace.clicked.connect(self.RaceGen)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "D&D Character Generater"))
        self.GenerateRace.setText(_translate("MainWindow", "Generate Race"))
        self.GenerateClass.setText(_translate("MainWindow", "Generate Class"))
        self.GenerateSubrace.setText(_translate("MainWindow", "Generate Subrace"))
        self.GenerateSubclass.setText(_translate("MainWindow", "Generate Subclass"))
        self.GenerateCharacter.setText(_translate("MainWindow", "Generate Character"))

    def RaceGen():
        CR=random.randint(1, 9)
        if CR==1:
            CR="Dwarf"
        elif CR==2:
            CR="Dragonborn"
        elif CR==3:
            CR="Elf"
        elif CR==4:
            CR="Gnome"
        elif CR==5:
            CR="Half-Elf"
        elif CR==6:
            CR="Half-Orc"
        elif CR==7:
            CR="Halfling"
        elif CR==8:
            CR="Human"
        elif CR==9:
            CR="Tiefling"
        print("{}".format(CR))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
It's a D&D character generator I've been working on (don't worry, I have no plans of trying to gain profit from it). It's supposed to be a random generator. As the first test of the random generation, I licked the generate race button to the function RaceGen. What I'd been hoping is that when that button was clicked it would generate a number which would then assign a string to the variable CR then print that (in the shell, not the main window, that's my next step).
I've tried getting it to put the generated string directly into the main window by changing the function RaceGen to:
def RaceGen():
    CR=random.randint(1, 9)
    if CR==1:
        CR="Dwarf"
    elif CR==2:
        CR="Dragonborn"
    elif CR==3:
        CR="Elf"
    elif CR==4:
        CR="Gnome"
    elif CR==5:
        CR="Half-Elf"
    elif CR==6:
        CR="Half-Orc"
    elif CR==7:
        CR="Halfling"
    elif CR==8:
        CR="Human"
    elif CR==9:
        CR="Tiefling"
    self.CharacterRace.addItem(CR)
In both cases upon clicking the button the Main Window would close and the shell I would see RESTART: SHELL with a bunch of = on either side.
Quote
#2
The problem is that RaceGen has two arguments, 'self' because it is inside of a class, and 'event' to pass the clicked event details.

    def RaceGen(self, event):
        CR = ("Dwarf", "Dragonborn", "Elf", "Gnome", "Half-Elf", "Half-Orc", "Halfing", "Human", "Tiefling")
        print(f"{random.choice(CR)}")
Gribouillis likes this post
Quote
#3
It seems to me that RaceGen() needs a 'self' parameter
def RaceGen(self):
    items = '''
        Dwarf DragonBorn Elf Gnome Half-Elf
        Half-Orc Halfling Human Tiefling
    '''.strip().split()
    self.CharacterRace.addItem(random.choice(items))
@Alfalfa oh yes, perhaps 'event' too!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] my simple window code is not working antonmichel 8 1,694 Jan-29-2018, 06:24 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)