2016-12-07 1 views
0

파이썬 3.5.2와 함께 tkinter 8.6을 사용하고 있습니다. 확인란을 클릭하면 사용자가 항목 상자에 무언가를 쓸 수 있도록 GUI를 만들려고합니다. 그러나 나는 오류가 발생합니다. 아래는 제 코드입니다. 나는 write_in 체크 박스를 클릭했을 때 다음과 같은 오류를 얻고있다 그러나확인란 위젯을 사용하여 항목 위젯 상태를 사용 안 함으로 설정하지 않도록 설정하려면 어떻게해야합니까?

from tkinter import * 
from tkinter import ttk 

def quit(): 
    mainframe.quit() 

#Write in Function 
def write(): 
    write_in.state(['!disabled']) #Getting Error Here 

root = Tk() 
root.title("Voting Booth") 

#Global variables 
var = StringVar() 

#Create main widget 
mainframe = ttk.Frame(root, padding="3 3 12 12") 
mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) 
mainframe.columnconfigure(0, weight=1) 
mainframe.rowconfigure(0, weight=1) 

#Additional widgets 
ttk.Label(mainframe, textvariable=var, font=('bold')).grid(column=2, row=0) 
vote = ttk.Button(mainframe, text="Vote", command = quit).grid(column=4, row=5, sticky=E) 
don_box = ttk.Checkbutton(mainframe, text="Donald Trump").grid(column=2, row=1, sticky=W) 
stein_box = ttk.Checkbutton(mainframe, text="Jill Stein").grid(column=2, row=3, sticky=W) 
write_box = ttk.Checkbutton(mainframe, text="Write in:", command = write).grid(column=2, row=4, sticky=W) 
write_in = ttk.Entry(mainframe, width = 20, state=DISABLED).grid(column=3, row=4, sticky=W) 

#Setting variables and widgets 
var.set("Presidential Nominees") 

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5) 

root.mainloop(); 

: 여기

" File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__ 
return self.func(*args) 
File "./booth_gui.py", line 13, in write 
write_in.state(['!disabled']) 
AttributeError: 'NoneType' object has no attribute 'state' " 

답변

1

write_in 객체 때문에 실제로는 None입니다.

write_in = ttk.Entry(...).grid(column=3, row=4, sticky=W) 

그러나 grid 방법은 None 결과를 반환합니다

문제

는 같은 write_in을 선언하는 것입니다.

grid에 대한 호출은 위젯 선언에서 구분해야합니다 : 위젯의 상태를 변경하는 방법에 대해

write_in = ttk.Entry(...) 
write_in.grid(column=3, row=4, sticky=W) 

, 나는 작동하도록되어 정말 모르겠어요.

config (또는 configure) 위젯 방법으로 시도하고 NORMALstate 키워드를 설정합니다`self.vote = ttk.Button (자체 : 나는 다음 줄에 오류를 받고 있어요

write_in.config(state=NORMAL) 
+0

나는 이것을 시도했지만 쓰기 기능을 호출하는 체크 상자를 클릭 할 때 여전히 오류가 발생했습니다. 함수 자체를 변경해야하는지 또는 올바른 라이브러리를 가져 오지 않을지 확실하지 않습니다. – pdm

+0

나는 지금 tkinter를 사용할 수 없기 때문에 확실하지 않았습니다. 그리고 ttk를 모른다 ... 그것이 당신에게주는 오류는 무엇입니까? 'config'가 존재하지 않거나'state'가 유효하지 않거나'NORMAL'이 정의되지 않았습니까? –

+0

AttributeError : 'NoneType'객체에 'config'속성이 없습니다. – pdm

1

는 완벽한 솔루션입니다 : 당신은 NoneType 예외가

import tkinter 
from tkinter import ttk 

class MyApp: 
    def __init__(self): 
     self.root = tkinter.Tk() 
     self.root.title('Voting Booth') 

     self.labelvar = tkinter.StringVar() 
     self.votevar = tkinter.StringVar() 
     self.writevar = tkinter.StringVar() 

     self.mainframe = ttk.Frame(self.root, padding="3 3 12 12") 
     self.mainframe.grid(column=0, row=0, sticky='nsew') 
     self.mainframe.columnconfigure(0, weight=1) 
     self.mainframe.rowconfigure(0, weight=1) 

     self.label = ttk.Label(self.mainframe, textvariable=self.labelvar, font=('bold')) 
     self.vote = ttk.Button(self.mainframe, text='Vote', command=self.quit) 
     self.don_box = ttk.Radiobutton(self.mainframe, text='Donald Trump', variable=self.votevar, value='trump', command=self.write) 
     self.stein_box = ttk.Radiobutton(self.mainframe, text='Jill Stein', variable=self.votevar, value='stein', command=self.write) 
     self.write_box = ttk.Radiobutton(self.mainframe, text='Write in: ', variable=self.votevar, value='write', command=self.write) 
     self.write_in = ttk.Entry(self.mainframe, textvariable= self.writevar, width=20, state='disabled') 

     self.label.grid(column=2, row=0) 
     self.vote.grid(column=4, row=5, sticky='e') 
     self.don_box.grid(column=2, row=1, sticky='w') 
     self.stein_box.grid(column=2, row=3, sticky='w') 
     self.write_box.grid(column=2, row=4, sticky='w') 
     self.write_in.grid(column=3, row=4, stick='w') 

     self.labelvar.set('Presidential Nominees') 

     for child in self.mainframe.winfo_children(): 
      child.grid_configure(padx=5, pady=5) 

    def quit(self): 
     self.root.destroy() 

    def write(self): 
     if self.votevar.get() == 'write': 
      self.write_in['state'] = 'normal' 
     else: 
      self.write_in['state'] = 'disabled' 
      self.writevar.set('') 

    def run(self): 
     self.root.mainloop() 

MyApp().run() 
+0

. self.quote = ttk.Button (self.mainframe, text = 'Vote', command = self.quit) AttributeError : 'MyApp (myframe, text ='Vote ', command = self.quit) '객체에'quit'' 속성이 없습니다. – pdm

+0

흠, 나는 그 오류를 얻지 못합니다. 에디터일까요? 또한 전체 소스를 복사했는지 확인하십시오. –

관련 문제