如何使用tkinter按钮分别调用两个函数?

时间:2018-04-28 16:39:24

标签: python function user-interface button tkinter

我正在尝试在tkinter中创建一个基本游戏,以熟悉使用Python进行GUI编程。我并不是想让它变得高效,因此多次复制和粘贴相同的语句。我试图这样做,当用户点击按钮时,它会将按钮的文本更改为“X”或“O”,具体取决于轮到谁。在我的按钮中,我有command =后跟我的函数将文本更改为两个选项中的任何一个。但是,当我单击一个按钮时,整个按钮网格将更改为该文本。所以我试图找出是否有类似的东西我可以使用:

command =  lambda:[placeX() or placeO()] 

我的代码如下:

import tkinter as tk, sys as s

Startwindow = tk.Tk()
Startwindow.title("Tic-Tac-Toe")
Startwindow.resizable(0,0)
Startwindow.geometry("1200x600")
image = tk.PhotoImage(file="C:\\Users\\Joshua Brown\\Desktop\\tic tac toe\\download.gif")
renderImage = tk.Label(image=image)
renderImage.grid()

def exe():
    print("Game has exited.")
    Startwindow.destroy()
    s.exit("Requested to close.")

def start():
    print("Game window opening...")
    gameWindow = tk.Tk()
    gameWindow.title("Tic-Tac-Toe: Game Window")
    gameWindow.geometry("450x483")
    gameWindow.resizable(0,0)
    Startwindow.iconbitmap(r'C:\Users\Joshua Brown\Desktop\tic tac toe\tictactoe_H4a_icon.ico')

    board1 = tk.Button(gameWindow, text = "1", command = lambda:[placeX(), placeO()], height = 10, width = 20)
    board1.grid(row=0, column=0)
    board2 = tk.Button(gameWindow, text = "2", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board2.grid(row=1, column=0)
    board3 = tk.Button(gameWindow, text = "3", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board3.grid(row=2, column=0)
    board4 = tk.Button(gameWindow, text = "4", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board4.grid(row=0, column=1)
    board5 = tk.Button(gameWindow, text = "5", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board5.grid(row=1, column=1)
    board6 = tk.Button(gameWindow, text = "6", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board6.grid(row=2, column=1)
    board7 = tk.Button(gameWindow, text = "7", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board7.grid(row=0, column=2)
    board8 = tk.Button(gameWindow, text = "8", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board8.grid(row=1, column=2)
    board9 = tk.Button(gameWindow, text = "9", command = lambda:[placeX(),placeO()], height = 10, width = 20)
    board9.grid(row=2, column=2)

    def placeX():
        board1.config(text='X')
        board2.config(text='X')
        board3.config(text='X')
        board4.config(text='X')
        board5.config(text='X')
        board6.config(text='X')
        board7.config(text='X')
        board8.config(text='X')
        board9.config(text='X')


    def placeO():
        board1.config(text='O')
        board2.config(text='O')
        board3.config(text='O')
        board4.config(text='O')
        board5.config(text='O')
        board6.config(text='O')
        board7.config(text='O')
        board8.config(text='O')
        board9.config(text='O')
    gameWindow.mainloop()

    if board1 and board2 and board3 == 'X':
        print("YEAH")


Startwindow.iconbitmap(r'C:\Users\Joshua Brown\Desktop\tic tac toe\tictactoe_H4a_icon.ico')
startButton = tk.Button(Startwindow, text = "Start Game", command = start, height = 1, width = 20, bg = '#ff3333')
startButton.config(font =("helvectia", 20))
startButton.grid()
exitButton = tk.Button(Startwindow, text = "Exit Game", command = exe, height = 1, width = 20, bg = '#ff3333')
exitButton.config(font =("helvectia", 20))
exitButton.grid()
Startwindow.configure(background = "white")

Startwindow.mainloop()

2 个答案:

答案 0 :(得分:0)

问题在于placeX和placeO函数。你看,你所有的按钮都指的是一个功能(changeX / changeY),它将所有按钮改为X或Y.

def placeX():
    board1.config(text='X') #changes 1st button to x
    board2.config(text='X')
    board3.config(text='X')
    board4.config(text='X')
    board5.config(text='X')
    board6.config(text='X')
    board7.config(text='X')
    board8.config(text='X')
    board9.config(text='X') #and all the rest above it non-selectively, one click = change all

相反,你应该拥有的是每个按钮的单独功能

board1_placeX():
    board1.config(text='X') #changes only one of them

board2_placeX():
    board2.config(text='Y')

...and so on, define 9 of these, and another nine for placeO(), like board1_placeO() and so on

当你定义板按钮时,使用特定的功能,例如,如果我定义板一,我将使用board1_placeX()/ board1_placeO()

board1 = tk.Button(gameWindow, text = "1", command = lambda:[board1_placeX(), board1_placeO()], height = 10, width = 20)
board1.grid(row=0, column=0)

和董事会2 ......

board2 = tk.Button(gameWindow, text = "1", command = lambda:[board2_placeX(), board2_placeO()], height = 10, width = 20)
board2.grid(row=0, column=0)

......等等。希望这会有所帮助。

答案 1 :(得分:0)

像这样使用带有 lambda 的示例方法:

test_button = Button(text="your_text_button", command=lambda:[placeX(),placeO()])