0

So I'm using tkinter to create a button that displays a random item from a list. 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.

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.

Does anyone have a solution for this issue? Thanks in advance.

  • It looks like your code in Load() only runs once, but it sounds like you want it to run repeatedly. Is that accurate? – Sam Hazleton Aug 23 '17 at 16:45
  • I hadn't thought of it that way, thanks! I've solved the issue now by running a new function for every new random number. – Pickselated Aug 24 '17 at 12:54

1 Answers1

1

There is a lot of awkwardness in your code. You should respect the naming conventions. See PEP8

Using global:

global rnum

After analysis, It appears that rnum is not a global variable, or you don't use it as a global variable. Anyway, the global keyword is only useful inside a function. See this question Use of “global” keyword in Python

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

It sounds a list, but it is not! In fact, this is a string. So, you can write it:

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

To open a file, use a with statement. 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:

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

I don't see any "main loop" in your program. See an example in the doc.

Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • I've managed to solve the issue now by creating a new function that runs for every new random number. My code being awkward is due to never being properly taught python, I'm mostly self taught. Thanks for the link, I'll follow the naming conventions in the future. Can you explain the difference between using the 'with' statement versus just opening a file? – Pickselated Aug 24 '17 at 12:56
  • @Pickselated: The with statement explained: https://stackoverflow.com/q/3012488/1513933 – Laurent LAPORTE Aug 24 '17 at 13:00
  • Do you need more explanation? I suggest you to upvote and [accept](https://meta.stackexchange.com/a/5235/344471) my answer. – Laurent LAPORTE Sep 04 '17 at 21:50