Reputation: 37
You may or may not be familiar with a popular party game called "Werewolves" - the specifics of the game aren't important here but I'm trying to make a program which will decide teams for it.
I have the following code which is fairly simple - it consists of 3 methods which I'm looking to attach to buttons and then display in a window when the program is run.
The code is shown here:
from Tkinter import *
from random import randint # import random integer generator
playerList = []
numberOfWolves = 0
def setWolfNumber(x):
numberOfWolves = x
def listAdd(string):
playerList.append(string)
def chooseTeams():
werewolves = [] # list of wolves, populated later for printing
villagers = [] # same as wolves but for villiagers
for x in range(0,len(playerList)): # loop for the number of players. Each iteration chooses a random player and decides whether they're a werewolf or not. After 2 werewolves it just adds villagers.
random = randint(0,len(playerList)-1)
player = playerList[random]
decide = randint(0,1)
if decide == 0 and len(werewolves) < numberOfWolves:
werewolves.append(player)
else:
villagers.append(player)
playerList.remove(player)
for wolf in werewolves: #prints wolf names
print wolf + " is a werewolf"
seer = villagers[randint(0,len(villagers))-1] #decide seer, print that, make sure they won't be reprinted with villagers
villagers.remove(seer)
print seer + " is the seer"
doctor = villagers[randint(0,len(villagers))-1] #same as seer but with the doctor
villagers.remove(doctor)
print doctor + " is the doctor"
for villager in villagers: # print villagers
print villager + " is a villager"
top = Tk()
top.wm_title("Werewolves Team Chooser")
w = Button(top)
top.mainloop()
As I'm sure you can see, the code for the GUI is very much incomplete. Could someone show me how to display fields in the GUI, and attach them to buttons? Effectively what I'm looking for is a field for adding players, and a field for setting the number of werewolves; then one button that takes that information and decides the team - using the methods provided.
Any help would be greatly appreciated.
Bonus: How hard would it be to turn the finished product into an executable file so my friends can use it too?
Thanks very much!
Upvotes: 0
Views: 43
Reputation: 360
Your button isn't displaying because you have not called pack on it. Essentially, you created a button, but you haven't added it to the window. There are three layout managers that you can use to add "widgets" (Tkinter's name for things in the GUI). These are pack, grid, and place. For a simple window such as the one you are making, pack should work fine. When you do later projects that are larger, grid becomes a very useful tool. Once you add w.pack() to your code after you create the button, you'll see the button does not have a name. You can add this by adding text="some text" to the arguments in the constructor. Similarly you can add an action to the button using callback=function (note the lack of parenthesis) and bind it to an action. You can also create "fields" similar to how you made the button. Simple use Entry() instead and fill with the arguments you need (I would recommend looking up StringVar for information on retrieving input).
Making an exe out of the final project isnt too bad. Personally, I like to use PyInstaller which takes the name of your script and a few arguments and creates the whole thing for you. There are also other tool such as py2exe which do the same thing.
Good luck! (P.S. awesome game, hope it goes well!)
Upvotes: 1