我的wxpython程序突然无法正常工作

时间:2016-05-30 16:18:05

标签: windows wxpython

我为学校做了几个星期的自我计划..我已经完成了它。

出于某种原因,当我现在尝试运行该程序时,它不起作用,尽管它在最后一次尝试时运行良好。

我确信我没有改变代码中的任何内容,但总有一些东西发生了变化而且我没有注意到它。

这些天我需要把它传递给我的老师,我不知道这个程序有什么问题。

我很乐意在这里得到一些帮助..

这是代码:

import wx
import winsound
import wx.grid as gridlib
from random import randint

OPTIONS = [1, 2, 3, 4, 5, 6, 7, 8, 9, "DEL", 0, "SEND"]
# these are the events' IDs sent to a function when you click a button.
# the OPTIONS_ID is in the same order of OPTIONS.

OPTIONS_ID = [-31984,-31983,-31982,-31981,-31980,-31979, -31978, -31977, -31976, -31975, -31974, -31973, -31985]  # the built in wxpython IDs for the buttons


GAME_POSITION = (400, 100)
GAME_SIZE = [900, 600]

def RandomNum():
    count = 5
    while count > 4:
        num = randint(1000, 9999)
        digits = str(num)
        count = 0
        for digit in digits:
            for digit2 in digits:
                if digit == digit2:
                    count = count + 1
    return digits

class Frame(wx.Frame):  # class for all the frames in our game.
    def __init__(self, parent, id, title, pos, size):
            wx.Frame.__init__(self, parent, id, title, pos, size)
            self.panel = wx.Panel(self)
            self.fdf = wx.TextCtrl(self.panel, size=(275, 75), pos=(520, 20))
            self.count = 0
            self.turnsCounter = 0
            self.numbers = RandomNum()
            self.bulls = 0
            self.cows = 0
            self.counter_of_turns = 0
            self.check = False

            self.grid = gridlib.Grid(self.panel, pos = (85, 150), size=(323, 212))
            self.grid.CreateGrid(10, 3)
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.panel, 1, wx.EXPAND)
            sizer.Add(self.grid, 1, wx.EXPAND)
            self.panel.SetSizer(sizer)
            for i in range(10):
                for j in range(4):
                    self.grid.SetReadOnly(i, j)
            self.grid.SetColLabelValue(0, "guess")
            self.grid.SetColLabelValue(1, "cows")
            self.grid.SetColLabelValue(2, "bulls")


    def message_dialog(self, message, caption, style=wx.YES_NO, position=GAME_POSITION):
        if message != "":  # making sure not to execute a message if its empety
            message = wx.MessageDialog(None, message, caption, style, position)
            answer = message.ShowModal()
            if answer == wx.ID_YES:
                self.reset()
            else:
                self.Destroy()
        else:
            return -1
    # this function creates a textbox at a specific position with a specific size.
    def write(self, panel, txt, pos, size=20, font_family=wx.SWISS, font_style = wx.NORMAL,font_weight = wx.BOLD, underline = False, color=wx.WHITE):
        # create a textbox at a specific position with a specific size.
        your_txt = wx.StaticText(panel, -1, txt, pos)
        your_txt.SetFont(wx.Font(size,font_family,font_style,font_weight,underline))
        your_txt.SetForegroundColour(color)
    # same as above, just for a button.
    def create_button(self, panel, txt, position, width, height, color, disable):
        Size = wx.Size(width, height)
        self.button = wx.Button(panel, -1, txt, position, Size)
        self.button.SetBackgroundColour(color)
        self.border = wx.BoxSizer(wx.VERTICAL)
        self.border.Add(self.button)
        self.Bind(wx.EVT_BUTTON, lambda evt: self.OnButton(evt), self.button)
        if disable == True:
            self.button.Disable()

    def count_bulls(self, txtctrl, seria):
        for i in range(4):
            if seria[i] == txtctrl[i]:
                self.bulls += 1
        replacement = self.bulls
        self.bulls = 0
        return replacement

    def count_cows(self, txtctrl, seria):
        for i in range(4):
            if seria[i] != txtctrl[i] and seria[i] in txtctrl:
                self.cows += 1
        replacement = self.cows
        self.cows = 0
        return replacement

    def reset(self):
        self.fdf.Clear()
        self.grid.ClearGrid()
        self.count = 0
        self.turnsCounter = 0
        self.numbers = RandomNum()
        self.bulls = 0
        self.cows = 0
        self.counter_of_turns = 0
        self.check = False
        for child in self.panel.GetChildren():
            if child.GetLabel() != "SEND":
                child.Enable()
            else:
                child.Disable()
            if self.count == 0:
                if child.GetLabel() == "DEL" or child.GetLabel() == "0":
                    child.Disable()
    def OnButton(self, event):
        print repr(event.Id) + ","
        print self.numbers
        if event.Id in OPTIONS_ID:  # if indeed an option button was pressed
            exited = -1  # exited is 5100 if the user exited his dialog box
            # assigning the events to the button.
            for i in range(13):
                if event.Id != -31985 and event.Id != -31975 and event.Id != -31974 and event.Id != -31973 and event.Id == OPTIONS_ID[i]:
                    self.fdf.AppendText(str(OPTIONS[i]))
                    self.count += 1
            if event.Id == -31974:
                self.fdf.AppendText(str(OPTIONS[10]))
                self.count += 1
            if event.Id == -31985:
                self.reset()
            if event.Id == -31973:
                self.counter_of_turns += 1
                print self.numbers
                print self.fdf.GetValue()
                cows = self.count_cows(self.fdf.GetValue(), self.numbers)
                bulls = self.count_bulls(self.fdf.GetValue(), self.numbers)
                self.grid.SetCellValue(self.turnsCounter,0, self.fdf.GetValue())
                self.grid.SetCellValue(self.turnsCounter, 1, str(cows))
                self.grid.SetCellValue(self.turnsCounter, 2, str(bulls))
                self.fdf.Clear()
                self.count = 0
                if self.turnsCounter < 9:
                    self.turnsCounter += 1
                if bulls == 4:
                    self.check = True
                    winsound.PlaySound('The_Power_-_Snap_1_.wav', winsound.SND_ASYNC | winsound.SND_LOOP)
                    self.message_dialog("Well done! you won this game..\n You won the game in %s turns .. \n Play again ? " % self.counter_of_turns , "You won!")
                    winsound.PlaySound(None, 0)
            if event.Id == -31975:
                if self.count > 0:
                    self.count -= 1
                self.fdf.Remove(self.fdf.GetLastPosition()-1, self.fdf.GetLastPosition())
            if self.count == 4:
                for child in self.panel.GetChildren():
                    if isinstance(child, wx.Button):
                        try:
                            int(child.GetLabel())
                        except ValueError:
                            if child.GetLabel() == "SEND":
                                child.Enable()
                        else:
                            child.Disable()
            elif self.check == False:
                for child in self.panel.GetChildren():
                    if child.GetLabel() != "SEND":
                        child.Enable()
                    else:
                        child.Disable()
                    if self.count == 0:
                        if child.GetLabel() == "DEL" or child.GetLabel() == "0":
                            child.Disable()
            #for child in self.panel.GetChildren():
                #if isinstance(child, wx.Button):
                    #if child.GetLabel() in self.fdf.GetValue():
                        #child.Disable()
            if self.counter_of_turns == 10 and self.check == False:
                self.message_dialog("YOU LOST :( \n THE NUMBERS WERE %s \n  PLAY AGAIN ?" % self.numbers,"Bad news ..")

class Game(wx.App):
    def OnInit(self):  # upon game opening
        # I would like the options window to be the first window's parent
        # so I will first set up our options window:
        window = Frame(None, -1, "Good Luck!", GAME_POSITION, GAME_SIZE)
        first_panel = window.panel
        window.SetBackgroundColour(wx.BLACK)
        window.write(first_panel, "BULLS AND COWS!", (50, 50), size=(35))
        countX = 500
        countY = 100
        window.create_button(first_panel,"restart!", (50, 400), 100, 100, wx.WHITE, False)
        for option in OPTIONS:
            if str(option) == "SEND" or str(option) == "DEL":
                window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.GREEN, True)
            elif str(option) == "0":
                window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.WHITE, True)
            else:
                window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.WHITE, False)
            countX += 110
            if str(option) == "3" or str(option) == "6" or str(option) == "9":
                countY += 110
                countX = 500



        window.Show(True)
        return True


def main():
    MasterMind = Game()
    MasterMind.MainLoop()


if __name__ == '__main__':
    main()

请注意:

我将窗户升级到Windows 10几天自我,这意味着它自升级以来不起作用,如果它意味着什么。 (对不起,如果我的英语语法不太好,那不是我的母语......)。

1 个答案:

答案 0 :(得分:0)

在我的电脑上工作正常(Python 2.7.11和Windows 7)。如果您在另一个文件中导入此类,请确保调用main函数。