# Tkinter program that displays 28 numbers on the screen [closed]

I created a simple program that displays 28 numbers on the screen.

from Tkinter import *
import random

class Game(Frame):

def __init__(self, parent):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.parent.title("Centered window")
self.pack(fill=BOTH, expand=1)
self.centerWindow()

def centerWindow(self):

w = 850
h = 530

sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()

x = (sw - w)/2
y = (sh - h)/2
self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))

def main():

global tile_frame_column  # the labels that go on the screen of individual numbers
tile_frame_column = {}

global tiles_make_number  # dictionary of all the individual numbers
tiles_make_number = {}

global tile_frame_column_counter
tile_frame_column_counter=0

global tiles_make_number_counter
tiles_make_number_counter=0

global xpos
xpos=35

global ypos
ypos=50

tile_numbers = ['1', '2', '3', '4', '5', '6', '1', '2', '1', '2', '1', 'l', '1', '3', '4', '2', '1', '2', '1', '1', '2', '1', '1', '6', '4', '3', '2', '2']

root = Tk()
ex = Game(root)

global tile_frame_column_counter
global tiles_make_number_counter
global tile_frame_column
global xpos
global ypos

if not tile_numbers:
return

rand = random.choice(tile_numbers)
tile_frame_column[tile_frame_column_counter] = Button(root, text=rand, font="Helvetica 16 bold")

tile_frame_column[tile_frame_column_counter].place(x=xpos, y=ypos)
tile_numbers.remove(rand)  # remove that tile from list of tiles
xpos += 80
if (len(tiles_make_number) % 7 == 0) & (len(tiles_make_number) > 0):
xpos = 35
ypos += 80
tile_frame_column[tile_frame_column_counter].place(x=xpos, y=ypos)
xpos += 80
tiles_make_number[tiles_make_number_counter] = rand
tile_frame_column_counter += 1
tiles_make_number_counter += 1

root.mainloop()

if __name__ == '__main__':
main()


I want the main() function to be initialized, though. Whenever a game object is created I want it to automatically add the main function -- meaning add numbers to the screen. I think the way my code is now, when a game object is created it only creates the screen.

Any ideas?

## closed as unclear what you're asking by t3chb0t, Toby Speight, Graipher, Donald.McLean, Marc-AndreOct 24 '17 at 14:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Exactly what you're asking for is unclear. Since you're on Code Review SE, I would suggest leaving out the "all that is for later" stuff. That makes it sound like your question would be a better fit for Stack Overflow. Just tell us what the existing code does, and how we can help you. And what do you mean by "I want the main() function to be initialized"? – skrrgwasme Oct 15 '14 at 19:05

For most of your question you will need to ask stack overflow specific question on each thing you want to do.

For example: You could ask a question about how to change the display of a button when said button is clicked. This kind of question is best suited for stack overflow.

Lets take everything in your program and place it into a class. This will serve to make working with the code a much less painful process as we can work with class attributes and we do not need to worry about global variables. (Globals are bad Mmm Kaaa!)

Here is your code reworked into a class. For the rest you should ask a series of question on stack overflow :)

A few notes: Try to be more consistent with naming conventions. ClassName, functionName, variable_name. This will make the code easier to read and maintain. You might also want to change self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y)) to use the .format() method instead as it is the correct method going forward and % is being fazed out.

from Tkinter import *
import random

class Game(Frame):

def __init__(self, parent):
Frame.__init__(self, parent, background="white")

self.parent = parent
self.parent.title("Centered window")
self.pack(fill=BOTH, expand=1)
self.centerWindow()

self.tile_frame_column = {}
self.tiles_make_number = {}
self.tile_frame_column_counter=0
self.tiles_make_number_counter=0
self.xpos=35
self.ypos=50

self.tile_numbers = ['1', '2', '3', '4', '5', '6', '1', '2', '1', '2', '1', 'l', '1', '3', '4', '2', '1', '2', '1', '1', '2', '1', '1', '6', '4', '3', '2', '2']

def centerWindow(self):
w = 850
h = 530
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - w) / 2
y = (sh - h) / 2
self.parent.geometry('%dx%d+%d+%d'%(w, h, x, y))

if not self.tile_numbers:
return
rand = random.choice(self.tile_numbers)
self.tile_frame_column[self.tile_frame_column_counter] = Button(root, text=rand, font="Helvetica 16 bold")

self.tile_frame_column[self.tile_frame_column_counter].place(x=self.xpos, y=self.ypos)
self.tile_numbers.remove(rand)  # remove that tile from list of tiles
self.xpos += 80
if (len(self.tiles_make_number) % 7 == 0) & (len(self.tiles_make_number) > 0):
self.xpos = 35
self.ypos += 80
self.tile_frame_column[self.tile_frame_column_counter].place(x=self.xpos, y=self.ypos)
self.xpos += 80
self.tiles_make_number[self.tiles_make_number_counter] = rand
self.tile_frame_column_counter += 1
self.tiles_make_number_counter += 1

'l' in tile_numbers[12] is not a number. Unless you want to participate in the underhanded Python coding contest, replace it with a '1'. But even if you wanted, there are better and less obvious ways of hiding evilness.