簡體   English   中英

隨機 function 無法正確啟動。 如果我點擊按鈕,文本不會改變,但如果我關閉並重新打開應用程序,它會改變

[英]The random function does not start correctly. If I click on the button the text doesn't change, but if I close and reopen the app it changes

此腳本的目的是顯示單詞“della gara”或“del match”與“valida”或“valido”的組合,具體取決於男性或女性的語法規則。別擔心,它可以正常工作:) Don'不用擔心,即使文本是非英語語言,也沒有關系,因為它會安靜地打印。只需在 for 循環中應用隨機 function 和sinonimo_partita_random

問題:問題是當我單擊打印按鈕時隨機 function 無法正確啟動。 嘗試多次單擊“打印”按鈕,沒有任何反應。

我想顯示sinonimo_partita_random ,但如果我點擊它總是打印“della gara”或總是“del match”。 同時,如果我關閉並重新打開腳本,則會更改單詞。

單擊“打印”按鈕時,如何使隨機 function 功能齊全?

import random
from tkinter import ttk
import tkinter as tk
from tkinter import *

root = tk.Tk()
root.geometry("400x150")
                      
sinonimi_partita_diz = {

        "della gara": {"genere" : "femminile", "unità" : "singolare", "apostrofo" : "no"},
        "del match": {"genere" : "maschile", "unità" : "singolare", "apostrofo" : "no"},
        }

valido_a_diz = {
    
            "valido" : {
            "genere" : "maschile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },        

            "valida" : {
            "genere" : "femminile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },                
        }

####################

text = tk.Text(root,width=43,height=2)
text.place(x=15, y=50)

#THE PROBLEM IS HERE:
sinonimo_partita = ("della gara", "del match")
sinonimo_partita_random = random.choice(sinonimo_partita)

valido_a = ""
for key, value in valido_a_diz.items():
    if sinonimi_partita_diz[sinonimo_partita_random] == value:
        valido_a = key
        break

def print():
    text.delete(1.0,END)
    phrase = f"{sinonimo_partita_random} {valido_a}"
    text.insert(tk.END, phrase)

button2 = Button(root, text="Print", command = print)
button2.pack()
button2.place(x=15, y=100)

我更改了代碼的順序,因此random function 現在位於print() function 內,最后root.mainloop()丟失了。

import random
import tkinter as tk
from tkinter import *

def print():
    sinonimo_partita_random = random.choice(sinonimo_partita)
    valido_a = ""
    for key, value in valido_a_diz.items():
        if sinonimi_partita_diz[sinonimo_partita_random] == value:
            valido_a = key
            break
    text.delete(1.0,END)
    phrase = f"{sinonimo_partita_random} {valido_a}"
    text.insert(tk.END, phrase)
                      
sinonimi_partita_diz = {

        "della gara": {"genere" : "femminile", "unità" : "singolare", "apostrofo" : "no"},
        "del match": {"genere" : "maschile", "unità" : "singolare", "apostrofo" : "no"},
        }

valido_a_diz = {
    
            "valido" : {
            "genere" : "maschile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },        

            "valida" : {
            "genere" : "femminile",
            "unità" : "singolare",
            "apostrofo" : "no"
            },                
        }

sinonimo_partita = ["della gara", "del match"]

root = tk.Tk()
root.geometry("400x150")

text = tk.Text(root,width=43,height=2)
text.place(x=15, y=50)

button2 = Button(root, text="Print", command = print)
button2.pack()
button2.place(x=15, y=100)

root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM