【问题标题】:GUI in python programingpython编程中的GUI
【发布时间】:2011-03-22 12:26:22
【问题描述】:

fallowing 是 python 中的井字游戏代码,有人可以告诉我如何使用重置选项以 GUI 形式制作它,并显示谁最终获胜。喜欢 X 赢还是 O 赢?

棋盘 = " 1 | 2 | 3\n------------\n 4 | 5 | 6\n-----------\n 7 | 8 | 9"

。 棋盘=[1,2,3,4,5,6,7,8,9,1,4,7,2,5,8,3,6,9,1,5,9,3,5,7 ]

空格=范围(1,10)

def moveHandler(board,spaces,checkboard,player,n):

if player==1:
    check="X"
else:   
    check="O"

while spaces.count(n)==0:
    print "\nInvalid Space"
    n=playerinput(player)

spaces=spaces.remove(n)

board=board.replace(str(n),check)

for c in range(len(checkboard)):
    if checkboard[c]==n:
        checkboard[c]=check


status = checkwinner(checkboard,check)
return board,status

def checkwinner(checkboard,check): a,b,c=0,1,2

while a<=21:
    combo = [checkboard[a],checkboard[b],checkboard[c]]

    if combo.count(check) == 3:
        status =1
        break
    else:
        status =0

    a+=3
    b+=3
    c+=3

return status

def 播放器输入(播放器): 尝试: key = int(raw_input('\n\nPlayer ' + str(player) + ': 请选择空格'))

except ValueError:

    print "Invalid Space"

    key = playerinput(player)

return key

当真时:

player = len(spaces)%2 +1

if player == 1:

    player = 2

else:

    player =1

print "\n\n" + board

key = playerinput(player)

board,status =moveHandler(board,spaces,checkboard,player,key)

if status == 1:

    print '\n\nPlayer ' + str(player) + ' is the winner!!!'

    print board

    break

elif len(spaces)==0:

    print "No more spaces left. Game ends in a TIE!!!"


    print board

    break

else:

    continue

【问题讨论】:

    标签: python


    【解决方案1】:

    显然,您需要选择一个 GUI 工具包(Python 支持其中的许多工具包),使用它将板绘制为 3 x 3 方格,并更改 playerinput 函数以接受(例如)当前的输入玩家双击他或她想玩的空白方块。

    然后,您需要更改 print 语句以在 GUI 表面上显示信息。

    然而,如果游戏不尝试控制事件流,而是响应玩家发起的事件,游戏会更好——这才是真正的 GUI 应用程序应该完成的方式,而不是通过对某些界面进行最少的改造在本质上设计为命令行交互式过程的基础之上。

    这些任务中的每一个都是一项实质性的任务,尤其是我在最后一段中推荐的整体重构,并且完全取决于您选择的 GUI 工具包的细节——因此您可能希望从它开始,然后通常会中断将问题分成不同的子任务(“每个问题一个问题”,因为可能会出现许多后者;-)。

    有几个关于 Python 的 GUI 选择主题的 SO 问题,所以我建议你研究它们而不是问一个新问题。我个人最喜欢的是 PyQt(尽管越来越多的时候我只是做一个简单的基于浏览器的界面,只有一个本地服务器为其提供动力),但其他流行的包括 wxPython、Tkinter、PyGtk 和其他列出的 here -- 很高兴狩猎!

    【讨论】:

      【解决方案2】:

      查看python wiki 了解有关不同 GUI 工具包的信息。我建议查看wxPython,然后从那里开始

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-02
        • 2011-06-06
        • 2010-11-02
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多