我怎样才能做到每次我单击 tkinter 上的按钮时,它都会输出列表中的下一行

问题描述 投票:0回答:1

我一直在尝试制作一些小项目来开始使用 Tkinter,我想制作一组规则,每当您单击按钮时它都会输出下一条规则。请帮助我不知道如何做到这一点。

基本上,尽管我的知识有限,但当我输入它时,我尝试了一些几乎肯定是错误的东西。这是 10 行规则,我尝试将其放入一个列表中并使用一个函数来执行每一条规则。正如你所知,并没有真正发挥作用。

from tkinter import *

rules=Tk()
rules.title("Rules")
rules.geometry("200x200")

def click():
    rule_label1 = rule_label1(rules, text=("The rules are quite simple"))
    rule_label2 = rule_label2(rules, text=("You get to roll two dice. The total score of the dice is added onto your personal score so for example, if you roll a 5 and a 6, you would get 11"))
    rule_label3 = rule_label3(rules, text=("If the total that you get is an even number then excellent because you get an extra 10 points"))
    rule_label4 = rule_label4(rules, text=("Bad news if your total is an add number though because that is -5 points"))
    rule_label5 = rule_label5(rules, text=("Double rolls allow you to roll an extra dice which is also added onto your total"))
    rule_label6 = rule_label6(rules, text=("Scores cannot go below 0 at any point"))
    rule_label7 = rule_label7(rules, text=("There will be 5 rounds and the person whose total is the highest wins"))
    rule_label8 = rule_label8(rules, text=("In the rare case of a tie, a single dice roll will decide who wins overall"))
    rule_label9 = rule_label9(rules, text=("Enjoy!!!"))
    rule_label10 = rule_label10(rules, text=("You can now close this screen."))                      
    
button = Button(rules, text="Click for next line", font="calibri", padx=50, pady=50, command=click, fg="red", bg="Green")
button.pack()

rules.mainloop()

python tkinter button
1个回答
0
投票

欢迎来到SO!

  1. 首先将您的规则移至列表中;我称之为

    rule_texts

  2. 创建将显示文本的标签 (

    rule_label
    ) 并将其初始化为列表的第一个条目 (
    rule_texts[0]
    )。

  3. 初始化

    i = 1
    ;该索引将引用我们从
    rule_texts
    访问的索引。

  4. 定义

    click()
    ,将
    rule_label
    设置为
    i
    的第
    rule_texts
    索引,然后将
    i
    递增 1。

  5. (建议)在到达

    rule_text
    列表末尾时添加处理。

from tkinter import *

rules = Tk()
rules.title("Rules")
rules.geometry("1000x200")

rule_texts = [
    "The rules are quite simple",
    "You get to roll two dice. The total score of the dice is added onto your personal score so for example, if you roll a 5 and a 6, you would get 11",
    "If the total that you get is an even number then excellent because you get an extra 10 points",
    "Bad news if your total is an add number though because that is -5 points",
    "Double rolls allow you to roll an extra dice which is also added onto your total",
    "Scores cannot go below 0 at any point",
    "There will be 5 rounds and the person whose total is the highest wins",
    "In the rare case of a tie, a single dice roll will decide who wins overall",
    "Enjoy!!!",
    "You can now close this screen.",
]

rule_label = Label(
    rules,
    text=rule_texts[0],
    font="calibri",
    padx=50,
    pady=50,
    fg="black",
)
rule_label.pack()


i = 1


def click():
    global i
    rule_label.config(text=rule_texts[i])
    i += 1
    # if i == len(rule_texts):
        # button.config(state=DISABLED)
        # button.destroy()


button = Button(
    rules,
    text="Click for next line",
    font="calibri",
    padx=50,
    pady=50,
    command=click,
    fg="red",
    bg="Green",
)
button.pack()


rules.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.