2017-04-22 3 views
0

私は、テキストエントリから入力としてスキル名を取得し、入力されたすべてのスキルの対応する値を計算するプログラムを作成しています。プログラムにスキルを入力してシェルに印刷すると、オブジェクトとして表示されますか?なぜこれが起こり、どうすれば修正できますか?reprまたは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で開始しません。 Pythonでは最初の変数の名前を指定する必要はありません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)に適切なパラメータを渡すように、入力文字列値ボタンプッシュを処理する関数に渡されます。

上記のコードは、ユーザー入力を端末に表示します(2回目、渡されたユーザー入力から1回、リストの2番目のすべての項目)。

self.floorEle1Skills = []行をshowFrame()に配置すると、スキルの入力を収集するリストがリセットされます(入力の再開を可能にする)。

上記のコードでは、両方の問題を解決していますが、これは解決する必要のある問題がないことを意味するものではありません。

関連する問題