NameError:名称“win”未使用 tkinter python 定义

执行此代码后,我收到错误win未找到从不同函数运行tkinter很重要,因为这是作业


法典:


import tkinter as tk

from tkinter import *

def zui(kaj,saj):

    zun=kaj

    kaj=kaj+"=tk.Tk()"

    exec(kaj)

    saj=zun+".title('"+saj+"')"

    exec(saj)

def zabel(self,naj,iaj,oaj,baj,gaj,taj):

    spsp=self+"="+"Label("+naj+", text='"+iaj+"', bg='"+oaj+"', height="+gaj+", width="+taj+",fg='"+baj+"')"

    spsp=str(spsp)

    exec(spsp)

def zosition(qak,iak,nak):

    sspp=qak+".grid(row="+iak+", column="+nak+")"

    exec(sspp)

def zainzoop(tak):

    sft=tak+".mainloop()"

    exec(sft)

zui("win","zahid app")

zabel("label","win","hello world","white","black","4","10")

zosition("win","1","1")

zainzoop("win")

追踪:


Traceback (most recent call last):

  File "c:/PyProjects/Patient Data Entry/hello.py", line 20, in <module>

    zabel("label","win","hello world","white","black","4","10")

  File "c:/PyProjects/Patient Data Entry/hello.py", line 12, in zabel

    exec(spsp)

  File "<string>", line 1, in <module>

NameError: name 'win' is not defined


猛跑小猪
浏览 133回答 1
1回答

精慕HU

这是使用 tkinter 的一种非常奇怪的方式,无论如何我该判断谁。 采取一个参数来做出声明。这是执行官的样子:exec()globals()globalexec(spsp,globals())exec(sspp,globals())exec(sft,globals())最后,应该这样称呼:zosition()zosition("label","1","1")以防万一您对它通常的样子感到好奇:import tkinter as tkkaj = tk.Tk()kaj.title('zahid app')spsp = tk.Label(kaj,text='Hello World',bg='white',fg='black',height=4,width=10)spsp.grid(row=1,column=1)kaj.mainloop() #much easier right?
打开App,查看更多内容
随时随地看视频慕课网APP