简体   繁体   English

Python:将变量传递到已经运行的函数中

[英]Python: Passing a variable into an already running function

So I'm using tkinter to create a button that displays a random item from a list. 因此,我正在使用tkinter创建一个按钮,该按钮显示列表中的随机项目。 When pressed, it displays a new item from said list, by looping a random number generator until it creates a number different to the old one. 当按下时,它通过循环一个随机数生成器直到显示与旧数字不同的数字,来显示所述列表中的新项目。 As they're tkinter buttons, the creation of a new number has to be a function, and the button itself is inside a function. 因为它们是tkinter按钮,所以创建新数字必须是一个函数,并且按钮本身在函数内部。

I've found that the variable for the current random number is not refreshing properly. 我发现当前随机数的变量无法正确刷新。 The value is changing globally but not inside the function containing the button, so when said button runs the function to generate a new number, it checks it against the very first number generated, rather than the previous one. 该值在全局范围内更改,但不在包含该按钮的函数内部更改,因此,当该按钮运行该函数以生成新数字时,它将根据生成的第一个数字(而不是前一个数字)进行检查。

from tkinter import *
import random
global rnum

def Load():

    ListPath = (".\\Lists\\test.txt")
    f = open(ListPath, "r")
    LineList = f.readlines()
    rnum = random.randint(0,(LineList.__len__()-1))

    load = Tk()
    word = Button(load, text = LineList[rnum], command = lambda: NewRN(rnum, word, LineList), font = ("Calibri", 30))
    word.pack()



def NewRN(rnum, word, LineList):
    rnumold = rnum

    while(rnum == rnumold):
        rnum = random.randint(0,(LineList.__len__()-1))
        word.config(text = LineList[rnum])
    return(rnum)

Load()

When pressing the button, there is the chance it will display the same item from the list, as rnum is not being passed back into the function once it is already running. 按下按钮时,有可能会显示列表中的同一项目,因为rnum一旦运行就不会被传递回该函数。

Does anyone have a solution for this issue? 有人对此问题有解决方案吗? Thanks in advance. 提前致谢。

There is a lot of awkwardness in your code. 您的代码中有很多尴尬之处。 You should respect the naming conventions. 您应该遵守命名约定。 See PEP8 PEP8

Using global : 使用global

global rnum

After analysis, It appears that rnum is not a global variable, or you don't use it as a global variable. 经过分析, rnum似乎不是全局变量,或者您没有将其用作全局变量。 Anyway, the global keyword is only useful inside a function. 无论如何, global关键字仅在函数内部有用。 See this question Use of “global” keyword in Python 查看此问题在Python中使用“ global”关键字

ListPath = (".\\Lists\\test.txt")

It sounds a list , but it is not! 听起来很list ,但事实并非如此! In fact, this is a string. 实际上,这是一个字符串。 So, you can write it: 因此,您可以编写它:

ListPath = ".\\Lists\\test.txt"

To open a file, use a with statement. 要打开文件,请使用with语句。 See Reading and Writing files . 请参阅读取和写入文件

with open(ListPath, "r") as f:
    LineList = f.readlines()

To get the length of a list, use the len() function: 要获取列表的长度,请使用len()函数:

rnum = random.randint(0, len(LineList) - 1)

I don't see any "main loop" in your program. 我在您的程序中看不到任何“主循环”。 See an example in the doc. 请参阅文档中的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM