【问题标题】:AttributeError: 'NoneType' object has no attribute '_root' (My case is different)AttributeError:“NoneType”对象没有属性“_root”(我的情况不同)
【发布时间】:2017-06-10 04:19:29
【问题描述】:
from tkinter import*
decision=0
fs = IntVar()
def coverscreen():
    slide=Tk() #Init window
    slide.title('The Castle of Redemption BETA') #Give window a title
    frame=Frame(slide)
    btn1=Button(slide,text='Start Game',command=slide.destroy) #Init 1st            button
    fsbox=Checkbutton(frame,text='Fullscreen',\
    variable=fs, onvalue=1,offvalue=0)
    img=PhotoImage(file='cover.gif') # Init picture
    label = Label(image=img) # Init label that contains picture
    label.image = img # keep a reference!
    label.pack() # Places the label on the window
    btn1.pack(side=BOTTOM,pady=5) # Places the 1st button on the window
    fsbox.pack()
    frame.pack(padx=50,pady=50)
    slide.mainloop() # Starts the window
def page(name,b1,b2,write,f,fscreen):
    slide=Tk() #Init window
    if fscreen == 1:
        slide.overrideredirect(True)
        slide.geometry("{0}x{1}+0+0".format(slide.winfo_screenwidth(),     slide.winfo_screenheight()))
    slide.title(name) #Give window a title
    btn1=Button(slide,text=b1,command=slide.destroy) #Init 1st button
    btn2=Button(slide,text=b2,command=slide.destroy) #Init 2nd button
    txt=Label(slide,text=write)# Init story text
    img=PhotoImage(file=f) # Init picture
    label = Label(image=img) # Init label that contains picture
    label.image = img # keep a reference!
    label.pack() # Places the label on the window
    btn1.pack(side=BOTTOM,pady=5) # Places the 1st button on the window
    btn2.pack(side=BOTTOM,pady=5) # Places the 2nd button on the window
    txt.pack(side=TOP,pady=5) # Places the text on the window
    slide.mainloop() # Starts the window
coverscreen()
page('Start','Continue','Go Back','Example Story Text.','cover.gif',fs.get()) #Example of the created function 'page'

我正在制作一个游戏,它的菜单在启动时出现并带有一个复选框,选中后会全屏打开游戏窗口。当我尝试运行程序时出现此错误:

AttributeError: 'NoneType' 对象没有属性 '_root'

【问题讨论】:

标签: python-3.x tkinter


【解决方案1】:

问题是您在创建根窗口Tk() 之前定义了fs = IntVar()

注意: 不建议创建多个根窗口Tk(),如果要显示第二个或更多窗口,请使用Toplevel 小部件。

如果您始终只想要一个窗口,则使用框架来包含寡妇内容,然后在使用相同的根目录时创建和销毁这些内容。

【讨论】:

  • 谢谢!我刚开始使用 Python,所以我猜“开始”按钮的命令应该是 frame.destroy? frame.destroy 命令是否也会破坏框架中的所有内容?
  • 另外,要让 fs 变量在 def(coverscreen) 之外工作,我会使用 global(fs) 对吗?
  • 是的,你必须定义一个全局的,最好看看编写 GUI 的类。否则,您可以查看lambda,以便您可以从coverscreen 内部调用page 函数并传递所需的值。
猜你喜欢
  • 1970-01-01
  • 2022-07-17
  • 2019-01-01
  • 2021-12-26
  • 2019-07-23
  • 2018-05-13
  • 2020-09-07
  • 2017-05-03
  • 2023-03-16
相关资源
最近更新 更多