Python:在类中初始化方法

时间:2014-10-02 04:09:07

标签: python class tkinter init

我创建了一个简单的程序,在屏幕上显示28个数字。目前,单击数字时没有任何反应。最终,我希望数字在点击时消失。此外,当你点击'5'时,你得到5分。我想制作这个多人游戏,10秒后得分最多的用户获胜。但这一切都是为了以后的事情。

我的代码是:

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)

    def add_number_to_screen():
        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.after(10, add_number_to_screen)

    root.after(10, add_number_to_screen)
    root.mainloop()


if __name__ == '__main__':
    main()

我希望初始化main()函数。每当创建游戏对象时,我希望它自动添加主要功能 - 意味着在屏幕上添加数字。我认为我的代码现在是这样的,当创建游戏对象时,它只会创建屏幕。但是必须有更好的方法来维护这段代码。还有一种更好的方法来编写这段代码,所以我不需要所有这些全局变量吗?

我尝试重做这样的代码,但它不起作用:

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.main()**

    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(self):**

        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)

        def add_number_to_screen():
            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.after(10, add_number_to_screen)

        root.after(10, add_number_to_screen)
        root.mainloop()

我改变的是我将self.main()添加到构造函数中,将main(self)放入类Game中并删除“if name =' main '但是当我运行程序时没有任何反应。有人有建议吗?

由于

1 个答案:

答案 0 :(得分:-1)

此链接可以帮助您:http://sebsauvage.net/python/gui/

它解释了如何使用python TkInterwxPython正确构建GUI,并会准确回答您的问题。

简而言之,最好让您的类继承自Tkinter.Tk并在if __name__ == "__main__":测试下启动主循环。