Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Help getting a new varible in a loop (or something)
#1
Hello, I have a coding question please.

This here code makes a simple (and ugly) tkinter bot GUI, but design isn't my question. I'm trying to figure out how to regenerate a new set varibles and print here:

	myLabel = Label(root, text=(a, b, e, c, d, f))
When I run the bot, it works but I need to figure out how to make a loop with each click to generate a new set of variables in the lable with each press of the Click me button.

This is just a random keyword generator using random words from the text files.

from tkinter import *

root = Tk()

import random


def wwwwwh(fname):
    lines = open(fname).read().splitlines()
    return random.choice(lines)

def common_conjugated(fname):
    lines = open(fname).read().splitlines()
    return random.choice(lines)

def noun(fname):
    lines = open(fname).read().splitlines()
    return random.choice(lines)

def verb(fname):
    lines = open(fname).read().splitlines()
    return random.choice(lines)

def adjective(fname):
    lines = open(fname).read().splitlines()
    return random.choice(lines)

def adverb(fname):
    lines = open(fname).read().splitlines()
    return random.choice(lines)

def myClick():
	myLabel = Label(root, text=(a, b, e, c, d, f))
	myLabel.pack()

a = (wwwwwh('WWWWWH.txt'))
b = (common_conjugated('common_conjugated_nouns.txt'))
c = (noun('91K_nouns.txt'))
d = (verb('31K_verbs.txt'))
e = (adjective('28K_adjectives.txt'))
f = (adverb('6K_adverbs.txt'))

myButton = Button(root, text="Click Me!", height=1, width=35, command=myClick)
myButton.pack()



button_quit = Button(root, text="Exit", command=root.quit)
button_quit.pack()

root.mainloop()
Thank you for any help.
Quote
#2
I'm not sure why this was moved to 'GUI' section, my problem was about coding but no worries ... I figured it out (I guess, could be better way?)

Because I couldn't get new random variables into variables in my button, I just packed the random variable directly into the lable.

It works now.

def myClick():
	myLabel = Label(root, text=(wwwwwh('WWWWWH.txt')) + " " + (common_conjugated('common_conjugated_nouns.txt')) + " " + (verb('31K_verbs.txt')) + " " + (adverb('6K_adverbs.txt')))
	myLabel.pack()
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)