【问题标题】:How can i change a variable in a definition如何更改定义中的变量
【发布时间】:2018-02-08 10:23:43
【问题描述】:

如何更改定义中的变量?我在互联网上搜索过,但找不到对我有帮助的东西。我发现全局变量可能会有所帮助,但我不知道如何使用它们,而且我还有另一个问题,我的代码甚至没有对我的“空格键”做出反应。这是我的完整代码,我知道他们写的不会发布你所有的代码,但也许这会对你有所帮助,因为我什至不明白什么是重要的,什么不是。(我知道我不应该一起使用 tkinter 和 pygame,但我懒得编码,我是只知道 tkinter 的菜鸟)。

from pygame import *
import pygame as pygame
from tkinter import *
import tkinter as tk
from time import sleep

#---------------------------------------

HEIGHT = 500

WIDTH = 500

window = Tk()

window.title("Test")

canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="grey")

canvas.pack(expand=False)

#---------------------------------------

a = (5, 5)
b = (85, 85)
c = (415, 415)
d = (495, 495)
e = (5, 415)
f = (85, 495)

player = canvas.create_rectangle([a, b], tags="obj1Tag", fill = "red4")
finish = canvas.create_rectangle([c, d], tags="obj2Tag", fill = "green4")
arrow = canvas.create_rectangle([e, f], tags="obj3Tag", fill = "blue4")

#---------------------------------------

mainloopon = True
start = False
start2 = False
startwindow = True
pms = 0.015625
arrow_direction = ("None")

#---------------------------------------

def start(event):
    start = True
    print ("start")

canvas.bind('<space>', start)

#---------------------------------------

colors = {"red":"blue4",
          "blue4":"yellow",
          "yellow":"green",
          "green":"red"}


def onObjectClick(event):
    current_color = canvas.itemcget(arrow, "fill")
    print (canvas.itemconfig(arrow, "fill"))
    canvas.itemconfig(arrow, fill=colors[current_color])

canvas.tag_bind(arrow, '<ButtonPress-1>', onObjectClick)

#---------------------------------------

while mainloopon == True:

    if startwindow == True:
        window.update()

        if start2 == True:
            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'blue4'):
                arrow_direction = ("right")

            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'red'):
                arrow_direction = ("left")


            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'yellow'):
                arrow_direction = ("up")

            if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'green'):
                arrow_direction = ("down")

    if start == True:
        canvas.move(player, 0, [pms])
        window.update()
        #sleep(0.0001)
        playerc = canvas.coords(player)
        arrowc = canvas.coords(arrow)

        if playerc == arrowc:
            start = False
            start2 = True

    if arrow_direction == ("right"):
        canvas.move(player, [pms], 0)
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("left"):
        canvas.move(player, [-pms], 0)
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("up"):
        canvas.move(player, 0, [-pms])
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("down"):
        canvas.move(player, 0, [pms])
        window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

window.mainloop()

编辑:

WIDTH = 500

window = Tk()

window.title("Test")

canvas = Canvas(window, width=WIDTH, height=HEIGHT, bg="grey")

canvas.pack(expand=False)

#---------------------------------------

a = (5, 5)
b = (85, 85)
c = (415, 415)
d = (495, 495)
e = (5, 415)
f = (85, 495)

player = canvas.create_rectangle([a, b], tags="obj1Tag", fill = "red4")
finish = canvas.create_rectangle([c, d], tags="obj2Tag", fill = "green4")
arrow = canvas.create_rectangle([e, f], tags="obj3Tag", fill = "blue4")

#---------------------------------------

mainloopon = True
start = False
start2 = False
startwindow = True
pms = 0.015625
arrow_direction = ("None")

#---------------------------------------

def change_start(event):
    global start
    start = True
    print (start)

canvas.bind('<space>', change_start)

#---------------------------------------

colors = {"red":"blue4",
          "blue4":"yellow",
          "yellow":"green",
          "green":"red"}


def onObjectClick(event):
    current_color = canvas.itemcget(arrow, "fill")
    print (canvas.itemconfig(arrow, "fill"))
    canvas.itemconfig(arrow, fill=colors[current_color])

canvas.tag_bind(arrow, '<ButtonPress-1>', onObjectClick)

#---------------------------------------

while mainloopon == True:

    if startwindow == True:
        window.update()

    if start2 == True:
        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'blue4'):
            arrow_direction = ("right")

        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'red'):
            arrow_direction = ("left")


        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'yellow'):
            arrow_direction = ("up")

        if canvas.itemconfig(arrow, "fill") == ('fill', '', '', '', 'green'):
            arrow_direction = ("down")

    if start == True:
        canvas.move(player, 0, [pms])
        #window.update()
        #sleep(0.0001)
        playerc = canvas.coords(player)
        arrowc = canvas.coords(arrow)

        if playerc == arrowc:
            start = False
            start2 = True

    if arrow_direction == ("right"):
        canvas.move(player, [pms], 0)
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("left"):
        canvas.move(player, [-pms], 0)
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("up"):
        canvas.move(player, 0, [-pms])
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

    if arrow_direction == ("down"):
        canvas.move(player, 0, [pms])
        #window.update()
        #sleep(0.01)
        playerc = canvas.coords(player)
        finishc = canvas.coords(finish)

        if playerc == finishc:
            print ("Finished")
            break

window.mainloop()

【问题讨论】:

  • 啊,如何在每行不添加 4 个空格的情况下制作代码块?
  • 对于代码块,您可以粘贴您的代码,然后突出显示代码,然后按类似于{} 或按下ctrl+k 的按钮,这会将所有内容移动到代码块中。
  • @HelpMe 看看我更新的答案。我的 PC 上没有安装 pygame,但是对于您在问题中谈到的 2 个问题,我的回答应该对您有用。

标签: python canvas tkinter pygame definition


【解决方案1】:

提供有关您的陈述的一些信息:

我发现全局变量可以提供帮助,但我不知道如何使用它们

这是一个简单的示例,说明如何从函数内部处理全局名称空间中的变量。

我们需要在每个需要使用全局变量的函数内部使用global 标识符。

some_var = "test"
some_var2 = "test2"

def change_global_var():
    global some_var, some_var2 # any variable names go here separated by a comma
    some_var = "Test Complete"
    some_var2 = "Test Complete"

change_global_var()

print(some_var, some_var2)

给你下一期:

代码甚至对我的“空格键”没有反应

问题与您有一个名为 start 的函数和一个变量有关。这不是一个好主意。此外,如果变量start 不是严格意义上的函数局部变量,您还需要告诉函数在哪里查找变量。您应该改为更改函数:

def start(event):
    start = True
    print ("start")

收件人:

# can be any function name just don't use names you have already used.
def change_start(event):

    global start # tells the function that start is in the global namespace
    start = True
    print ("start")

更新:

绑定和画布的一个问题是绑定将不适用于绘制的对象,除非绑定用于鼠标单击。这与focus 无法在绘制的对象上有关。因此,您需要将空格键绑定到根窗口。或者,如果您使用框架,画布所在的框架将起作用。

变化:

canvas.bind('<space>', change_start)

收件人:

window.bind('<space>', change_start)

这应该可以解决您的空格键问题。

【讨论】:

  • 还是不行我试过了,有很多人说你不应该使用全局变量(我不知道为什么)你能给我解释一下吗?并且谢谢
  • @HelpMe 在不使用类的情况下编写程序时有时需要使用全局变量,并且某些类型的全局变量很好。这是一个关于全局变量的好答案的链接,以及为什么它们被认为是坏的。 Why are global variables evil?
  • @HelpMe 以及您到底尝试了什么。您是更改了我提到的所有内容还是仅更改了全局变量?
  • 你在第二个代码块中写的一切。
  • @HelpMe 当您按空格键时,您是否至少将打印语句发送到控制台?
猜你喜欢
  • 2022-12-07
  • 1970-01-01
  • 2013-01-14
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2016-11-14
  • 1970-01-01
相关资源
最近更新 更多