+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2021-02-28 16:49     阅读(872)     评论(0)     点赞(3)     收藏(0)


0

1

2

3

4

5

6

7

8

我正在尝试创建一个tkinter程序来在按下按钮时修改标签。听起来很简单,但是我的代码无法正常工作。Python没有显示任何错误,它只是冻结/崩溃并且没有响应。我在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. 我正在使用的所有字体都在同一目录中并且可以正常工作。

当我运行代码时,有时会崩溃,有时label192甚至在我单击按钮之前,它会默认显示标签textey()为红色。请帮忙。我很乐意在评论中澄清对我的问题的任何疑问。为什么在程序加载后立即显示为红色?您能在您的机器上运行它,然后告诉我它是否仍然无法正常工作吗?代码中是否有错误?即使在prmp()调用函数之前,该文本仍为红色

另外,如果有帮助,请参见以下mmstraphGL中的tubn类的代码:

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文件具有ext:.pyw,请尝试将其更改为.py,以使控制台靠近。并记录下来,如果崩溃了。顺便说一句,如果他们没有登录终端。这意味着您在使用此功能时遇到了过滤问题:

def remember(self):
        tubb.destroy()

我搜索了代码,并定义了所有按钮。尝试擦除这些行,然后重试。

0

1

2

3

4

5

6

7

8

9



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/863991/e573877156c3a97d503b/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)