2017-04-22 1 views
0

필자는 텍스트 입력에서 스킬 이름을 입력으로 사용하고 입력 한 모든 기술의 해당 값을 계산하는 프로그램을 작성 중입니다. 프로그램에 스킬을 입력 한 다음 셸에 스킬을 인쇄하면 오브젝트로 나타납니다. 왜 이런 일이 일어나고 어떻게 해결할 수 있습니까? 대표 또는 str이 필요합니까? 텍스트 입력을 삭제하는 delete 메소드가 작동하지 않는 이유는 무엇입니까?python에서 tkinter를 사용하여 인쇄 할 때 입력이 객체로 나타나는 이유는 무엇입니까?

import tkinter as tk 
from tkinter import ttk 

#make the lists to store the skill names 
floorEle1Skills = [] 

class startValue(tk.Tk): 

    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) 

     tk.Tk.wm_title(self, "Start Value Calculator") 
     tk.Tk.minsize(self, width = 350, height = 300) 

     container = tk.Frame(self) 
     container.pack(side = 'top', fill = 'both', expand = True) 
     container.grid_rowconfigure(0, weight = 1) 
     container.grid_columnconfigure(0, weight = 1) 

     self.frames = {} 

     for f in (startPage, floorPage, pommelPage, ringsPage, vaultPage, pbarsPage, hbarPage): 

      frame = f(container, self) 

      self.frames[f] = frame 

      frame.grid(row = 0, column = 0, sticky = "nsew") 

     self.showFrame(startPage) 

     #make the lists to store the skill names 
     floorEle1Skills = [] 

    def showFrame(self, cont): 

     frame = self.frames[cont] 
     frame.tkraise() 

    def floorEle1(skill): 
     floorEle1Skills.append(skill) 
     #clear the text entry 
     #ele1Entry.delete(0, tk.END) 
     #why doesnt this work??? 
     #why is it printed as an object?? 
     print(floorEle1Skills) 


class startPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text = "Select Event") 
     label.pack(pady = 10, padx = 10) 

     floorButton = ttk.Button(self, text = "Floor", command = lambda : controller.showFrame(floorPage)) 
     floorButton.pack() 


class floorPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text = "Floor") 
     label.pack(pady = 10, padx = 10) 

     #make the entries and labels 
     ele1Label = tk.Label(self, text = "Element Group 1:") 
     ele1Label.pack() 
     skill1 = tk.StringVar() 
     ele1Entry = tk.Entry(self, textvariable = skill1) 
     ele1Entry.pack() 
     ele1Button = ttk.Button(self, text = "Add", command = lambda : controller.floorEle1()) 
     ele1Button.pack() 

     startButton = ttk.Button(self, text = "Back to Start", command = lambda : controller.showFrame(startPage)) 
     startButton.pack(side = 'bottom') 
+0

내 대답에서 코드를 사용해 보셨나요? 질문에서 언급 한 문제를 해결합니다. – Claudio

답변

0

파이썬에 오신 것을 환영합니다. 문제는 floorEle1(skill)입니다. 이것은 class startValue의 멤버 함수이지만 인수 목록은 self로 시작하지 않습니다. 파이썬은 첫 번째 변수의 이름을 지정하지 않습니다. self; 실제로 원하는 이름으로 부를 수 있습니다 (하지만하지 마십시오!). 따라서이 함수 내에서 skill이라는 변수는 변수 self처럼 작동합니다. 그것은 정확히 것처럼 당신이 쓴 것 :

def floorEle1(self): 
    floorEle1Skills.append(self) 
    #clear the text entry 
    #ele1Entry.delete(0, tk.END) 
    #why doesnt this work??? 
    #why is it printed as an object?? 
    print(floorEle1Skills) 

나는 당신이 당신의 코드가 사실상 floorEle1Skills에 self를 추가하는 것이 지금은 볼 수 있다고 생각합니다; 즉, 메인 윈도우의 인스턴스를 추가하십시오! 따라서 목록을 인쇄 할 때 print 문은 목록에 개체가 있음을 보여줍니다.

0

이미 다른 답변에서 언급 한 것처럼 코드의 문제는 floorEle1(self, skill)을 돌아서지만 ... 입력 된 기술을 기술 목록에 전달하려면 적절하게 해결해야하는 몇 가지 다른 문제가 있습니다. (아래 코드 참조) 코드에

import tkinter as tk 
from tkinter import ttk 

#make the lists to store the skill names 
# floorEle1Skills = [] 

class startValue(tk.Tk): 

    def __init__(self, *args, **kwargs): 

     tk.Tk.__init__(self, *args, **kwargs) 

     tk.Tk.wm_title(self, "Start Value Calculator") 
     tk.Tk.minsize(self, width = 350, height = 300) 

     container = tk.Frame(self) 
     container.pack(side = 'top', fill = 'both', expand = True) 
     container.grid_rowconfigure(0, weight = 1) 
     container.grid_columnconfigure(0, weight = 1) 

     self.frames = {} 

     for f in (startPage, floorPage): # , pommelPage, ringsPage, vaultPage, pbarsPage, hbarPage): 

      frame = f(container, self) 

      self.frames[f] = frame 

      frame.grid(row = 0, column = 0, sticky = "nsew") 

     self.showFrame(startPage) 

     #make the lists to store the skill names 
     self.floorEle1Skills = [] 

    def showFrame(self, cont): 

     self.floorEle1Skills = [] 
     frame = self.frames[cont] 
     frame.tkraise() 

    def floorEle1(self, skill): 
     print("#", skill.get()) 
     self.floorEle1Skills.append(skill) 
     #clear the text entry 
     #ele1Entry.delete(0, tk.END) 
     #why doesnt this work??? 
     #why is it printed as an object?? 
     for item in self.floorEle1Skills: 
      print("##",item.get()) 


class startPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text = "Select Event") 
     label.pack(pady = 10, padx = 10) 

     floorButton = ttk.Button(self, text = "Floor", command = lambda : controller.showFrame(floorPage)) 
     floorButton.pack() 


class floorPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 
     label = tk.Label(self, text = "Floor") 
     label.pack(pady = 10, padx = 10) 

     #make the entries and labels 
     ele1Label = tk.Label(self, text = "Element Group 1:") 
     ele1Label.pack() 
     skill1 = tk.StringVar() 
     ele1Entry = tk.Entry(self, textvariable = skill1) 
     ele1Entry.pack() 
     ele1Button = ttk.Button(self, text = "Add", command = lambda : controller.floorEle1(ele1Entry)) 
     ele1Button.pack() 

     startButton = ttk.Button(self, text = "Back to Start", command = lambda : controller.showFrame(startPage)) 
     startButton.pack(side = 'bottom') 

root = tk.Tk() 
my_gui = startValue() 
root.mainloop() 

다른 변경됩니다

'__ INIT __() 함수 self.floorEle1Skills = []의 정의와 controller.floorEle1(ele1Entry)에 적절한 파라미터를 전달되도록 상기 입력 문자열 값 버튼 누름을 처리하는 함수로 전달됩니다.

위의 코드는 사용자 입력을 터미널에 인쇄합니다 (두 번, 전달 된 사용자 입력에서 두 번째, 목록의 두 번째 항목 모두).

self.floorEle1Skills = [] 라인을 showFrame()에 배치하면 스킬 입력을 수집하는 목록이 재설정됩니다 (입력 재시작 가능).

위의 코드는 문제에서 해결 된 두 가지 문제를 해결하지만 해결할 필요가있는 추가 문제가 있음을 의미하지 않습니다.

관련 문제