You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python tkinter界面切换故障:destroyu2r按钮无响应

Tkinter掷骰子游戏:destroyu2r函数无响应问题

在基于Python Tkinter开发的掷骰子游戏中,destroyu2r函数无法关闭user2roll界面并切换到对应功能界面,点击触发该函数的按钮毫无反应,游戏流程无法推进。

相关代码片段

def destroyu2r():
    user2roll.destroy()
    global destroyu2r
    global score1
    r1=random.choice(d6)
    r2=random.choice(d6)
    r3=random.choice(d6)
    destroyu2r=tk.Tk()
    destroyu2r.geometry("640x480")
    destroyu2r.title(user1)
    tk.Label(destroyu2r,text=("Your first roll... A",r1,"!")).pack()
    tk.Label(destroyu2r,text="").pack()
    tk.Label(destroyu2r,text=("And your second roll is... A",r2,"1")).pack
    tk.Label(destroyu2r,text="").pack
    if r1 == r2:
        score1=score1+r1+r2+r3+10
        tk.Label(destroyu2r,text=("As your first two rolls were the same, you get an extra 10 points added to your score and your third roll is..",r3,"!")).pack()
        tk.Label(destroyu2r,text=("Your total score is now... ",score1,"!")).pack()
        tk.Label(destroyu2r,text="").pack()
        tk.Button(destroyu2r,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()
    else:
        roll=r1+r2
        if roll in odd:
            score1=score1+r1+r2-5
            tk.Label(destroyu2r,text=("You rolled  ",r1," and ", r2,". Together they add up to ",roll,". As ",roll," is an odd number, ",r1," and ",r2," will be added to your score, then 5 points will be deducted.")).pack()
            tk.Label(destroyu2r,text=("You total score is now... ",score1,"!")).pack()
            tk.Label(destroyu2r,text="").pack()
            tk.Button(destroyu2r,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()
            if score1 <= 0:
                destroyfu1r()
                gameendscreen()
        else:
            score1=score1+r1+r2+10
            tk.Label(destroyu2r,text=("You rolled  ",r1," and ", r2,". Together they add up to ",roll,". As ",roll," is an even number, ",r1," and ",r2," will be added to your score, then 10 points will be added.")).pack()
            tk.Label(destroyu2r,text=("Your total score is now... ",score1,"!")).pack()
            tk.Label(destroyu2r,text="").pack()
            tk.Button(destroyu2r,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()

完整代码

import random
import time
import tkinter as tk
import os
from turtle import goto

score1=0
score2=0
d6=[1,2,3,4,5,6]
even=[2,4,6,8,10,12]
odd=[1,3,5,7,9,11]

def destroyu2r():
    user2roll.destroy()
    global destroyu2r
    global score1
    r1=random.choice(d6)
    r2=random.choice(d6)
    r3=random.choice(d6)
    destroyu2r=tk.Tk()
    destroyu2r.geometry("640x480")
    destroyu2r.title(user1)
    tk.Label(destroyu2r,text=("Your first roll... A",r1,"!")).pack()
    tk.Label(destroyu2r,text="").pack()
    tk.Label(destroyu2r,text=("And your second roll is... A",r2,"1")).pack
    tk.Label(destroyu2r,text="").pack
    if r1 == r2:
        score1=score1+r1+r2+r3+10
        tk.Label(destroyu2r,text=("As your first two rolls were the same, you get an extra 10 points added to your score and your third roll is..",r3,"!")).pack()
        tk.Label(destroyu2r,text=("Your total score is now... ",score1,"!")).pack()
        tk.Label(destroyu2r,text="").pack()
        tk.Button(destroyu2r,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()
    else:
        roll=r1+r2
        if roll in odd:
            score1=score1+r1+r2-5
            tk.Label(destroyu2r,text=("You rolled  ",r1," and ", r2,". Together they add up to ",roll,". As ",roll," is an odd number, ",r1," and ",r2," will be added to your score, then 5 points will be deducted.")).pack()
            tk.Label(destroyu2r,text=("You total score is now... ",score1,"!")).pack()
            tk.Label(destroyu2r,text="").pack()
            tk.Button(destroyu2r,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()
            if score1 <= 0:
                destroyfu1r()
                gameendscreen()
        else:
            score1=score1+r1+r2+10
            tk.Label(destroyu2r,text=("You rolled  ",r1," and ", r2,". Together they add up to ",roll,". As ",roll," is an even number, ",r1," and ",r2," will be added to your score, then 10 points will be added.")).pack()
            tk.Label(destroyu2r,text=("Your total score is now... ",score1,"!")).pack()
            tk.Label(destroyu2r,text="").pack()
            tk.Button(destroyu2r,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()

def user1roll():
    global user1roll
    global score1
    r1=random.choice(d6)
    r2=random.choice(d6)
    r3=random.choice(d6)
    user1roll=tk.Tk()
    user1roll.geometry("640x480")
    user1roll.title(user1)
    tk.Label(user1roll,text=("Your first roll... A",r1,"!")).pack()
    tk.Label(user1roll,text="").pack()
    tk.Label(user1roll,text=("And your second roll is... A",r2,"1")).pack
    tk.Label(user1roll,text="").pack
    if r1 == r2:
        score1=score1+r1+r2+r3+10
        tk.Label(user1roll,text=("As your first two rolls were the same, you get an extra 10 points added to your score and your third roll is..",r3,"!")).pack()
        tk.Label(user1roll,text=("Your total score is now... ",score1,"!")).pack()
        tk.Label(user1roll,text="").pack()
        tk.Button(user1roll,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()
    else:
        roll=r1+r2
        if roll in odd:
            score1=score1+r1+r2-5
            tk.Label(user1roll,text=("You rolled  ",r1," and ", r2,". Together they add up to ",roll,". As ",roll," is an odd number, ",r1," and ",r2," will be added to your score, then 5 points will be deducted.")).pack()
            tk.Label(user1roll,text=("You total score is now... ",score1,"!")).pack()
            tk.Label(user1roll,text="").pack()
            tk.Button(user1roll,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()
            if score1 <= 0:
                destroyu1r()
                gameendscreen()
        else:
            score1=score1+r1+r2+10
            tk.Label(user1roll,text=("You rolled  ",r1," and ", r2,". Together they add up to ",roll,". As ",roll," is an even number, ",r1," and ",r2," will be added to your score, then 10 points will be added.")).pack()
            tk.Label(user1roll,text=("Your total score is now... ",score1,"!")).pack()
            tk.Label(user1roll,text="").pack()
            tk.Button(user1roll,text=(user2,"'s turn."),height="2",width=480,command=user2roll).pack()

def destroyu1r():
    user1roll.destroy()

def destroyfu1r():
    destroyu2r.destroy()

def gameendscreen():
    print("HI")
    
def destroyfu2r():
    user2roll.destroy()

def user2roll():
    destroyu1r()
    global user2roll
    global score2
    r4=random.choice(d6)
    r5=random.choice(d6)
    r6=random.choice(d6)
    user2roll=tk.Tk()
    user2roll.geometry("640x480")
    user2roll.title(user2)
    tk.Label(user2roll,text=("Your first roll... A",r4,"!")).pack()
    tk.Label(user2roll,text="").pack()
    tk.Label(user2roll,text=("And your second roll is... A",r5,"1")).pack()
    tk.Label(user2roll,text="").pack()
    if r4 == r5:
        score2=score2+r4+r5+r6+10
        tk.Label(user2roll,text=("As your first two rolls were the same, you your rolls, ",r4," and ",r5," and you get an extra 10 points added to your score and your third roll is... ",r6,"!")).pack()
        tk.Label(user2roll,text=("Your total score is now... ",score2,"!")).pack()
        tk.Label(user2roll,text="").pack()
        tk.Button(user2roll,text=(user1,"'s turn."),height="2",width=480,command=destroyu2r).pack()
    else:
        roll=r4+r5
        if roll in odd:
            score2=score2+r4+r5-5
            tk.Label(user2roll,text=("You rolled  ",r4," and ", r5,". Together they add up to ",roll,". As ",roll," is an odd number, ",r4," and ",r5," will be added to your score, then 5 points will be deducted.")).pack()
            tk.Label(user2roll,text=("You total score is now... ",score2,"!")).pack()
            tk.Label(user2roll,text="").pack()
            tk.Button(user2roll,text=(user1,"'s turn."),height="2",width=480,command=destroyu2r).pack()
            if score2 <= 0:
                destroyfu2r()
                gameendscreen()
        else:
            score2=score2+r4+r5+10
            tk.Label(user2roll,text=("You rolled  ",r4," and ", r5,". Together they add up to ",roll,". As ",roll," is an even number, ",r4," and ",r5," will be added to your score, then 10 points will be added.")).pack()
            tk.Label(user2roll,text=("Your total score is now... ",score2,"!")).pack()
            tk.Label(user2roll,text="").pack()
            tk.Button(user2roll,text=(user1,"'s turn."),height="2",width=480,command=destroyu2r).pack()
user1roll()

内容的提问来源于stack exchange,提问作者آدم إسماعيل ظفر جليسي

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 13:06:17