我的 tkinter 程序在按下按钮时修改标签出现故障(关闭)

My tkinter program to modify a label when a button is pressed is malfunctioning (closed)

我正在尝试创建一个 tkinter 程序来在按下按钮时修改标签。听起来很简单,但我的代码不起作用。 Python 没有显示任何错误,它只是 freezes/crashes 并且没有响应。我在 windows 10 上使用 pycharm。 我已经复制了我所有的代码并粘贴在下面:

我的这部分代码并不重要,但可能会有用:

import socket
import mmstraphGL
from tkinter import Tk, PhotoImage
import numpy as keras
import time
import waiting
import pyglet
import tkinter as mykinter
import sys
import pyautogui as tkinter

testmat1 = [1, 2]
testmat2 = [2, 3]
print(keras.dot(testmat1, testmat2))


def socket_get():
    return socket.gethostbyname(socket.gethostname())


copyrightx = sys.copyright

scwidth, scheight = tkinter.size()
widit = scwidth * ((4 - 0.1 + (1 - 1)) / 5)
hihit = scheight * ((4 - 0.1 + (1 - 1)) / 5)
ww = ((10 - 2) / ((6 * 1) + (2 - 1))) * widit
hh = (9 / (9 - 1)) * hihit
diwit = int(ww)
tihit = int(hh)

rootgeo = (str((str(int((5 / 5) * scwidth))) + 'x' + (str(int((5 / 5) * scheight)))))

root: Tk = mykinter.Tk()
root.geometry(rootgeo)

iconx = mykinter.PhotoImage(file='icon.png.png')
root.iconphoto(False, iconx)

x_Left = int(root.winfo_screenwidth() / 2 - diwit / 2)
y_Top = int(root.winfo_screenheight() / 2 - tihit / 2)
root.geometry("+{}+{}".format(x_Left, y_Top))

root.title("T squared")
root.attributes('-fullscreen', False)
bagr = mykinter.PhotoImage(file='converted.gif')
bagger = mykinter.Label(root, image=bagr, height=tihit * 2, width=scwidth * 2).place(x=-776, y=0)


def center(win):
    win.update_idletasks()
    width = win.winfo_width()
    frm_width = win.winfo_rootx() - win.winfo_x()
    win_width = width + 2 * frm_width
    height = win.winfo_height()
    titlebar_height = win.winfo_rooty() - win.winfo_y()
    win_height = height + titlebar_height + frm_width
    x = win.winfo_screenwidth() // 2 - win_width // 2
    y = win.winfo_screenheight() // 2 - win_height // 2
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))
    win.deiconify()


center(root)


def create_font(self):
    pyglet.font.add_file(self)


def fonts():
    create_font('good times rg.ttf')
    create_font('dindin.ttf')
    create_font('f4o8nt.ttf')
    create_font('minefalse.ttf')


lvl = 1

fonts()

frame12 = mykinter.Frame(highlightbackground='#283747', master=root, highlightthickness=10, relief='raised')
frame12.place(x=0, y=0)

我的代码中 真正 使其发生故障的部分是:

label12 = mmstraphGL.lab(True, 'underline', "Text here", 55, "Good Times Rg", '#EBDEF0', True, True, '#E74C3C', 0, 0,
                         [7, mykinter.RAISED], root, 500,
                         500,
                         ((1.2 / 2) * diwit), 35)
label24 = mykinter.Label(text="Text here text here", font=('.', 27), fg='#2E4053',
                         borderwidth=9.5, relief=mykinter.RAISED,
                         master=root, padx=((2.2 / 6.3) * (1 / 2) * diwit), pady=40)
label24.place(x=0, y=297)

label48 = mykinter.Label(
    text="Text here text here text here text here",
    font=('Omotenashi 2', 22), fg='#EBDEF0', bg='#212F3C', borderwidth=6.5, relief=mykinter.RAISED, master=root,
    padx=((5.4 / 6.3) * (1 / 2) * diwit), pady=25)
label48.place(x=0, y=460)
label192 = mykinter.Label(text="textey", font=('.', 8), fg='#2E4053',
                          borderwidth=9.5, relief=mykinter.RAISED,
                          master=root, padx=0, pady=0)
label192.place(x=760, y=920)


def prmp():
    global label192
    label192.config(fg='#FF0000')


nottub96 = mmstraphGL.tubn(mykinter.RAISED, True, "bold underline", False, '#EBDEF0', '#E74C3C',
                           "Make textey red", 10, mykinter.NORMAL, prmp(),
                           'Serreria Sobria',
                           '#283747', True, True, '#EBDEF0', ((8.2 / 12) * (diwit / 2)), 795, 9, root, 250, 1600, 5, 2)


def waitfuncs():
    def check(murda):
        if murda:
            return True
        else:
            return False

    def wait_until(delegate, timeout: int):
        end = time.time() + timeout
        while time.time() < end:
            if delegate():
                return True
            else:
                time.sleep(0.0067)
        return False


root.mainloop()

几点注意事项:

  1. 我知道mmstraphGL不是模块,我自己做的。
  2. 我使用的所有字体都在同一个目录中并且工作正常。

当我 运行 时,我的代码有时会崩溃,有时甚至在我单击按钮之前显示标签 textey(label192) 默认为红色。请帮忙。我很乐意在评论中澄清对我的问题的任何疑问。为什么程序一加载就变红了?你可以 运行 在你的机器上安装它并告诉我它是否仍然不能工作吗?代码有没有错误?即使在函数 prmp() 被调用之前,文本也是红色的。

此外,如果有帮助,这里是 mmstraphGL 中 tubn class 的代码:

class tubn:
    def __init__(self, sigh, downlinspace, cascsheet, actsdef, actbg, actfg, txt, fomtsize, defstate, cmd, fomtype,
                 hhexx_fg,
                 borderstate, hhexx_bg_truestate, hhexx_bg, yex_pos, why_pos, bordr, masta, hght, wdth, xp, yp):
        if actsdef:
            if (hhexx_bg_truestate == False) and (borderstate == False) and (downlinspace == False):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=lambda: cmd, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == False) and (downlinspace == False):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=lambda: cmd, bg=hhexx_bg, master=masta,
                                       text=str(txt),
                                       font=(fomtype, fomtsize), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == False) and (borderstate == True) and (downlinspace == False):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=lambda: cmd, bd=bordr, master=masta,
                                       text=str(txt),
                                       font=(fomtype, fomtsize), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == True) and (downlinspace == False):
                tubb = mykinter.Button(state=defstate, command=lambda: cmd, bg=hhexx_bg, bd=bordr, master=masta,
                                       text=str(txt), relief=sigh, font=(fomtype, fomtsize), fg=str(hhexx_fg),
                                       height=int(int(hght) / 2), width=int(int(wdth) / 2), padx=xp, pady=yp).place(
                    height=int(int(hght) / 2), width=int(int(wdth) / 2), x=yex_pos, y=why_pos)
            elif (hhexx_bg_truestate == False) and (borderstate == False) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=lambda: cmd, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize, cascsheet), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == False) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, command=lambda: cmd, bg=hhexx_bg, relief=sigh, master=masta,
                                       text=str(txt),
                                       font=(fomtype, fomtsize, cascsheet), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == True) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, command=lambda: cmd, bg=hhexx_bg, bd=bordr, master=masta,
                                       text=str(txt), relief=sigh, font=(fomtype, fomtsize, cascsheet),
                                       fg=str(hhexx_fg),
                                       height=int(int(hght) / 2), width=int(int(wdth) / 2), padx=xp, pady=yp).place(
                    height=int(int(hght) / 2), width=int(int(wdth) / 2), x=yex_pos, y=why_pos)
            elif (hhexx_bg_truestate == False) and (borderstate == True) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, command=lambda: cmd, relief=sigh, bd=bordr, master=masta,
                                       text=str(txt),
                                       font=(fomtype, fomtsize, cascsheet), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
        elif not actsdef:
            if (hhexx_bg_truestate == False) and (borderstate == False) and (downlinspace == False):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=lambda: cmd, activebackground=actbg,
                                       activeforeground=actfg, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == False) and (downlinspace == False):
                tubb = mykinter.Button(relief=sigh, state=defstate, command=lambda: cmd, activebackground=actbg,
                                       activeforeground=actfg, bg=hhexx_bg, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == False) and (borderstate == True) and (downlinspace == False):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=lambda: cmd, activebackground=actbg,
                                       activeforeground=actfg, bd=bordr, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == True) and (downlinspace == False):
                tubb = mykinter.Button(state=defstate, command=lambda: cmd, activebackground=actbg,
                                       activeforeground=actfg, relief=sigh, bg=hhexx_bg, bd=bordr, master=masta,
                                       text=str(txt),
                                       font=(fomtype, fomtsize), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == False) and (borderstate == False) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, command=lambda: cmd, activebackground=actbg,
                                       activeforeground=actfg, relief=sigh, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize, cascsheet), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == False) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=cmd, activebackground=actbg,
                                       activeforeground=actfg, bg=hhexx_bg, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize, cascsheet), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == True) and (borderstate == True) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, relief=sigh, command=lambda: cmd, activebackground=actbg,
                                       activeforeground=actfg, bg=hhexx_bg, bd=bordr, master=masta, text=str(txt),
                                       font=(fomtype, fomtsize, cascsheet), fg=str(hhexx_fg), height=int(int(hght) / 2),
                                       width=int(int(wdth) / 2), padx=xp, pady=yp).place(height=int(int(hght) / 2),
                                                                                         width=int(int(wdth) / 2),
                                                                                         x=yex_pos,
                                                                                         y=why_pos)
            elif (hhexx_bg_truestate == False) and (borderstate == True) and (downlinspace == True):
                tubb = mykinter.Button(state=defstate, command=lambda: cmd, activeforeground=actfg, bd=bordr,
                                       master=masta, relief=sigh, text=str(txt), font=(fomtype, fomtsize, cascsheet),
                                       fg=str(hhexx_fg), height=int(int(hght) / 2), width=int(int(wdth) / 2), padx=xp,
                                       pady=yp).place(height=int(int(hght) / 2), width=int(int(wdth) / 2), x=yex_pos,
                                                      y=why_pos)

        self.text = txt
        self.size = fomtsize
        self.font = fomtype
        self.color = hhexx_fg
        self.x_position = yex_pos
        self.y_position = why_pos
        self.border = bordr
        self.height = hght
        self.width = wdth

    def update(self, configee):
        tubb.config(configee)

    def remember(self):
        tubb.destroy()

提前致谢!

编辑:我的疑惑解开了!你不需要打扰!

我有一个问题:没有控制台日志就关闭了?如果您的 python 文件有扩展名: .pyw 尝试将其更改为 .py 以使控制台靠近。并在崩溃时记录下来。顺便说一句,如果他们在终端上没有日志。这意味着您的函数有过滤问题:

def remember(self):
        tubb.destroy()

我在代码中进行了搜索,发现任何按钮都会调用该定义。尝试擦除这些线条并重试。