0

I am something of a newbie to Python and Kivy but I've made enough progress with Python to write a character generator program for roleplaying games. On the command line, the script works fine. With Kivy, I can get as far as producing a selection list of radio buttons and then clicking a Next button to generate the character. The output of this then goes to the terminal window from which I ran the python script. I've looked all over this site and many others to find out how to display the character stats in the Kivy window.

    #
    # BRP Character Generator
    #

    import sys
    from kivy.app import App
    from kivy.uix.label import Label
    from kivy.uix.checkbox import CheckBox
    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.button import Button
    from kivy.properties import StringProperty, ListProperty, DictProperty
    from kivy.uix.widget import Widget

    from brp_stats import *
    from dice_roller import *

    race = ''
    statblock = ''

    class CharacterGenerator(GridLayout):

        statblock = DictProperty({})

        def leave(x):
            print('Exiting BRP Character Generator')
            sys.exit()

       def human(self,a,b):
            if b==True: 
                self.Status="human"
                race=self.Status
                print (race)
                statblock = human()
                characteristic_rolls = rolls(statblock)
                skill_category_modifiers = scm(statblock)
                skill_category_bonuses = scb(statblock)
                printcharacter(self, statblock,characteristic_rolls,skill_category_modifiers,skill_category_bonuses)

More character races go here and the end of the script looks like this:

    class CharacterGeneratorApp(App):

        def build(self):
            self.title="BRP Character Generator"
            return CharacterGenerator()

    if __name__ == '__main__':
        CharacterGeneratorApp().run()

The charactergenerator.kv looks like this:

    <CharacterGenerator>:
        cols: 2
        canvas:
            Color:
                rgb: .2,.2,.2
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: 'Human'
        CheckBox:
            group: 'race_group'
            on_active: root.human(*args)

        Button:
            text: 'Next'
            on_press: root.printcharacter()

The brp_stats.py module contains all the print functions. One example is the print_stats function:

def print_stats(mystats): print ("Characteristics") statlist = [ 'STR' , 'CON' , 'SIZ' , 'INT' , 'POW' , 'DEX' , 'APP' , 'EDU' ] print (" ", end="") for stat in statlist: print (stat," [", mystats[stat], "]", sep="", end= " ") print ()

This works fine on the command line and prints its results to the terminal from which the python script was run. The (mystats) parameter is a python dictionary with this format:

{'CON': 13, 'POW': 7, 'STR': 10, 'SIZ': 16, 'INT': 12, 'EDU': 7, 'DEX': 14, 'APP': 14}

How do I get the mystats dictionary displayed in a Kivy Widget?

Regards, Colin Brett

Colin Brett
  • 47
  • 2
  • 6
  • Set the content of a kivy widget (e.g. a label or a textinput or what works for you) to the string you want to output. – syntonym Jul 13 '16 at 14:16
  • @syntonym So something like? self.add_widget(Label(text=mystats)) , where mystats is the name of the array I want printed? I'm afraid that doesn't work: the output is blank. I get the plain string mystats if I use self.add_widget(Label(text='mystats')). I may have misunderstood your answer. – Colin Brett Jul 13 '16 at 15:20
  • You don't get an error if you set `text=mystats`? `mystats` is a list and I would have thought that kivy complained when it doesn't get a string there. But nevermind: You need to provide a string. You can use `str(mystats)` to output a string representation of the list or use a more sophisticated formatting method e.g. `", ".join(mystats)` (see also [here](http://stackoverflow.com/questions/2906092/converting-a-list-to-a-string)). – syntonym Jul 13 '16 at 15:32
  • @syntonym . That fixed it, thanks. I had to rejig the print_stats function to `def print_stats(mystats): s = '' print ("Characteristics") statlist = [ 'STR' , 'CON' , 'SIZ' , 'INT' , 'POW' , 'DEX' , 'APP' , 'EDU' ] print (" ", end="") for stat in statlist: # print (stat," [", mystats[stat], "]", sep="", end= " ") s = s + " [" + stat + str(mystats[stat]) + "]" # print () return s ` . Then I made a call to that function using `printable_stats = print_stats(my_sb)` and printed with `self.add_widget(Label(text='Stats' + str(printable_stats)))` – Colin Brett Jul 13 '16 at 17:10

0 Answers0