在Tkinter中的函数内更改变量 [英] Changing Variables within a Function in Tkinter

查看:128
本文介绍了在Tkinter中的函数内更改变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望X和O动画在单击鼠标时来回切换。问题出在函数XorO中。我不太清楚为什么,但是只有在单击它时才会创建X。我认为这可能与我写转弯变量的方式有关。这就是我所拥有的。

I want the X and O animations to switch back and forth upon mouse clicks. The problem is in the function XorO. I don't really understand why, but it will only create Xs when I click it. I think it may have to do with how I wrote the turn variable. Here is what I have.

from tkinter import *


tk = Tk()
width = 600
third = width / 3
canvas = Canvas(width=width, height=width)
tk.title = ("Tic Tac Toe")


line1 = canvas.create_line(200, 0, 200, 600)
line2 = canvas.create_line(400, 0, 400, 600)
line3 = canvas.create_line(0, 200, 600, 200)
line4 = canvas.create_line(0, 400, 600, 400)



def mouse_click(event):
    col = int(event.x / third)
    row = int(event.y / third)
    XorO(row, col)

def XorO(row,col):
    class XsorOs:
        turn = 1
        if turn is (1 or 3 or 5 or 7 or 9):
            canvas.create_line(col * third, row * third, (col + 1) * third, (row + 1) * third)
            canvas.create_line((col + 1) * third, row * third, col * third, (row + 1) * third)

        else:
            canvas.create_oval(col * third + 5, row * third + 5, (col + 1) * third - 5, (row + 1) * third - 5)
        turn += 1

canvas.pack()
canvas.bind("<Button-1>", mouse_click)
canvas.mainloop()


推荐答案

这里的问题是每次调用 XorO 方法时,都会创建 XsorOs 对象。这意味着 XsorOs.turn 始终为1。
一种方法是从外部跟踪 turn 并使用 global 调用它,但使用? = global#global rel = nofollow noreferrer>全局是应该避免的事情,特别是它可能会变得很凌乱。
我建议在自己的Tk单独的子类逻辑类中跟踪转弯

The problem here is that the XsorOs object gets created every time you call the XorO method. This means XsorOs.turn is always 1. One way would be to keep track of turn from outside and call it with global but the use of global is something one should avoid especially it can get quite messy. I would recommend keeping track of turn within an own child class of Tk separate "logic" class

我为您提供了后者的示例:

I made you an example for the latter:

(请注意,该示例非常草率(尤其是变量命名),应该只显示我的意思)

(please note that this example is super sloppy (particularly the variable naming) and should just show you what I meant)

# stays the same until 'line4 = canvas.create_line(0, 400, 600, 400)'
class XsorOs:
    def __init__(self):
        self.turn = 1

    def click(self, row, col):
        if self.turn is (1 or 3 or 5 or 7 or 9):
            canvas.create_line(col * third, row * third, (col + 1) * third, (row + 1) * third)
            canvas.create_line((col + 1) * third, row * third, col * third, (row + 1) * third)
        else:
            canvas.create_oval(col * third + 5, row * third + 5, (col + 1) * third - 5, (row + 1) * third - 5)
            self.turn += 1


def mouse_click(c, event):
    col = int(event.x / third)
    row = int(event.y / third)
    c.click(row, col)


xo = XsorOs()
canvas.pack()
canvas.bind("<Button-1>", lambda event: mouse_click(xo, event))
canvas.mainloop()

编辑:


  • lambda 基本上是一种创建单行函数的方法。在这种情况下,我使用它通过事件函数传递参数。因为在内部 tkinter 会执行类似的操作,如果发生鼠标单击,请执行passed_function(event),这样您就没有机会使用您的自己的论点。这就是为什么 lambda 在这里有用的原因

  • lambda is basically a way to create one line functions. In this case, I used it to pass arguments through the event function. Because somewhere internally tkinter does something like if that mouseclick happens do passed_function(event) so you have no chance to use your own arguments. That's why lambda is useful here

__ init __ 可能在这里并不是那么重要,因为我之前看到人们在类主体中放置变量,并且显然工作得很好,但是我个人更喜欢它在构造函数中创建类的所有变量

__init__ is maybe not that important here since I saw people putting variables in class bodies before and apparently it works just fine, but I personally prefer it to create all the variables of a class in the constructor

self 类似于 this 在其他语言中对类或对象的引用该类(实际上,您可以通过命名第一个构造函数参数来命名,但是通常使用 self )。它在类的范围内而不是在函数范围内拉变量。这意味着变量存在,只要对象存在,就可以对其进行操作。函数执行后基本上会丢失所有内容。那是先前代码中的主要问题。

self is like this in other languages a reference to the class or the object of that class (you can actually name it the way you want by naming the first constructor argument, but self is commonly used). It "pulls" the variable in the scope of the class instead of the function. That means the variable exists and can be manipulated as long as the object exists. A function basically loses everything after execution. That was the main problem in your prior code.

这篇关于在Tkinter中的函数内更改变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆