-3

How can I make that in label generate some numbers for example:

some text(e.g.Input): #here generate numbers#

Input and Output are text in label I want to make that number are generated every second in the label

4

3 回答 3

3

要在每个随机数之间等待一秒钟,请使用该after方法并更改预定函数中的文本:

from tkinter import Tk, Label
import random

root = Tk()
label = Label(root)
label.pack()

def replace_text():
    label.config(text=str(random.random()))
    root.after(1000, replace_text)

replace_text()
root.mainloop()
于 2013-04-21T19:37:24.937 回答
1

这是你要找的吗?每按一次按钮,就会显示一个新的随机数。

import tkinter as tk
import random

class Window:
    def __init__(self, master):
        self.frame = tk.Frame(master)
        self.text = tk.StringVar()
        self.text.set(random.randint(1, 10))
        self.ranNumLabel = tk.Label(self.frame, textvariable = self.text)
        self.genButton = tk.Button(self.frame, text = 'Generate Random Number', command = self.genRanNum)
        self.ranNumLabel.grid(row = 0)
        self.genButton.grid(row = 1)
        self.frame.grid()

    def genRanNum(self):
        self.text.set(random.randint(1, 10))
        # when text is updated, the Label associated with it also updated

def main():
    root = tk.Tk(className = ' Random Number Generator')
    app = Window(root)
    root.mainloop()

if __name__ == '__main__':
    main()
于 2013-04-21T16:58:19.483 回答
0

您的问题相当令人困惑,但这是我可以回答的...要生成随机数,请使用random.random()函数和label()tkinter 中的函数。

这是一个例子:

import random
from tkinter import *
x=random.random()

root = Tk()

w = Label(root, text=x)
w.pack()

root.mainloop()
于 2013-04-21T16:58:28.593 回答