5 Replies - 216 Views - Last Post: 15 July 2019 - 03:15 PM Rate Topic: -----

#1 Lavacorn430   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-July 19

python & tkinter how to assign each random number a label to spawn

Posted 15 July 2019 - 02:08 PM

So I'm trying to make an app to generate a dnd character. I have everything except the stats! I can generate 6 but I can't get them t print separately. How can I get each variable to be assigned to its own button?

using python and tkinter

#need black bg and white and light grey letters
#1 button
#alliance,race,class slots
#6 stat slots
from tkinter import *
import random
from random import randint
from tkinter import ttk
from PIL import ImageTk, Image
import sys
import os
from tkinter import filedialog

str1 = ['Barbarian', 'Bard', 'Cleric', 'Druid', 'Fighter', 'Monk', 'Paladin', 'Ranger', 'Rogue', 'Sorcerer', 'Warlock',
        'Wizard']
clas = str1

str2 = ['Acolyte', 'Charlatan', 'Criminal', 'Entertainer', 'Folk Hero', 'Gladiator', 'Guild Artisan', 'Guild Merchant',
        'Hermit', 'Knight', 'Noble', 'Outlander', 'Pirate', 'Sage', 'Sailor', 'Soldier', 'Spy', 'Urchin']
background = str2

str3 = ['Chaotic Evil', 'Chaotic Good', 'Chaotic Neutral', 'Lawful Evil', 'Lawful Good', 'Lawful Neutral',
        'Neutral Evil', 'Neutral Good',
        'True Neutral']
alligm = str3

str4 = ['Aarakokra', 'Aasimar', 'Air Genasi', 'Bugbear', 'Deep Gnome', 'Dragonborn', 'Drow', 'Dwarf', 'Earth Genasi',
        'Fire Genasi', 'Firbolg', 'Forest Gnome', 'Githyanki', 'Githzerai', 'Goblin', 'Half-elf', 'Halfling',
        'Half-orc', 'High Elf', 'Hobgoblin', 'Human', 'Kenku', 'Kobold', 'Lizardfolk', 'Orc', 'Rock Gnome', 'Tabaxi',
        'Tiefling', 'Triton', 'Warforged', 'Water Genasi', 'Wood Elf', 'Yuan-ti']
race = str4


def rollstats():
    statslist = []
    for h in range(6):
        h = h
        total = []
        for a in range(4):
            a = a
            numm = randint(1, 6)
            total.append(numm)
        total.remove(min(total))
        totalnum = 0
        for x in total:
            totalnum += x
        statslist.append(totalnum)
    return statslist
nowstats = rollstats()



master = Tk()
root = master
canvas = Canvas(root, width = 300, height = 420, bg = 'black')
canvas.pack()

Anchor = 'nw'
var = StringVar()
label = Label( root, font=("ArielNarrowBold", 18),  textvariable=var, relief=RAISED, fg='white', bg='black')
var.set("We're building a...")
label.place(relx=0, rely=0, relwidth=1, relheight=0.1)

#C:\Users\Lavacorn430\Pictures\SWR\SWRbutton.png
photo = PhotoImage(file=r"C:\Users\Lavacorn430\Pictures\SWR\SWRbutton.png")
Button(root, image=photo).pack(side=TOP)

#Character info

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielNarrowBold", textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set((random.choice(alligm)))
label.place(relx=0.1, rely=0.11, relwidth=0.8, relheight=0.09)

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielNarrowBold", textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set((random.choice(race)))
label.place(relx=0.1, rely=0.21, relwidth=0.8, relheight=0.09)

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielNarrowBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set((random.choice(clas)))
label.place(relx=0.1, rely=0.31, relwidth=0.8, relheight=0.09)

Anchor = 'nw'
var = StringVar()
label = Label( root, font=("ArielNarrowBold", 15),  textvariable=var, relief=RAISED, fg='white', bg='black')
var.set('Stats')
label.place(relx=0, rely=0.41, relwidth=1, relheight=0.09)

#stats

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set(nowstats)
label.place(relx=0.075, rely=0.51, relwidth=.25, relheight=0.07)

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set(nowstats)
label.place(relx=0.375, rely=0.51, relwidth=.25, relheight=0.07)

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set('Con')
label.place(relx=0.675, rely=0.51, relwidth=.25, relheight=0.07)

nchor = 'nw'
var = StringVar()
label = Label( root, font="ArielBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set('Int')
label.place(relx=0.075, rely=0.61, relwidth=.25, relheight=0.07)

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set('Wis')
label.place(relx=0.375, rely=0.61, relwidth=.25, relheight=0.07)

Anchor = 'nw'
var = StringVar()
label = Label( root, font="ArielBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
var.set('Cha')
label.place(relx=0.675, rely=0.61, relwidth=.25, relheight=0.07)




root.mainloop()


Is This A Good Question/Topic? 0
  • +

Replies To: python & tkinter how to assign each random number a label to spawn

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15226
  • View blog
  • Posts: 60,944
  • Joined: 12-June 08

Re: python & tkinter how to assign each random number a label to spawn

Posted 15 July 2019 - 02:17 PM

Make an array of how ever many stat numbers you need. Use a loop to generate a new value (with a given max), and assign to the current array indexed spot. Repeat.
Was This Post Helpful? 0
  • +
  • -

#3 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 553
  • View blog
  • Posts: 1,730
  • Joined: 27-December 13

Re: python & tkinter how to assign each random number a label to spawn

Posted 15 July 2019 - 02:27 PM

I see you make 6 buttons; I don't understand your question.
Is it about button text or button action?
Please explain you problem in more details.

In line 37 and 40 you do h = h and a = a. Those lines are hardly necessary.
Similar with the list names, first you do strx = [...] and the name = strx.
Why not name the list as they are made, i.e. name = [...]?

Line 44-46 can be written as totalsum = sum(total)
Was This Post Helpful? 0
  • +
  • -

#4 Lavacorn430   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-July 19

Re: python & tkinter how to assign each random number a label to spawn

Posted 15 July 2019 - 02:52 PM

View PostDK3250, on 15 July 2019 - 02:27 PM, said:

I see you make 6 buttons; I don't understand your question.
Is it about button text or button action?
Please explain you problem in more details.


I have 6 labels for the stats but I do not know how to make the generator run again to assign a new number to each label. at the moment it puts the same number in each slot.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11629
  • View blog
  • Posts: 19,760
  • Joined: 19-March 11

Re: python & tkinter how to assign each random number a label to spawn

Posted 15 July 2019 - 03:10 PM

Since your code is a bit convoluted, it might be useful if you can point out the line(s) where you expect numbers to be assigned to labels.
Was This Post Helpful? 0
  • +
  • -

#6 DK3250   User is online

  • Pythonian
  • member icon

Reputation: 553
  • View blog
  • Posts: 1,730
  • Joined: 27-December 13

Re: python & tkinter how to assign each random number a label to spawn

Posted 15 July 2019 - 03:15 PM

I'm not sure that this answers your question, but maybe some inspiration anyway:
#stats

for i in range(6):
    Anchor = 'nw'
    var = StringVar()
    label = Label( root, font="ArielBold",  textvariable=var, relief=RAISED, fg='white', bg='grey')
    var.set(nowstats[i])
    label.place(relx=0.075 +(i%3)*0.3, rely=0.51+0.1*(i//3), relwidth=.25, relheight=0.07)


root.mainloop()


The code replaces 96-130.
The idea is not to repeat yourself.
So, with a few simple formulas you 'pack' the code and simplify its maintenance.
If you need, you can place this snippet in a function and call it whenever required.

This post has been edited by DK3250: 15 July 2019 - 03:18 PM
Reason for edit:: minor formula simplification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1