为什么打印时的输入在python中使用tkinter表示为对象?

时间:2017-04-22 02:39:18

标签: python python-3.x tkinter representation tkinter-entry

我正在编写一个程序,它将技能名称作为文本条目的输入,并计算输入的所有技能的相应值。当我在程序中输入技能然后将技能打印到shell时它会显示为对象?为什么会发生这种情况以及如何解决?我需要 repr str 吗?为什么删除方法也不清除文本输入工作呢?

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')

2 个答案:

答案 0 :(得分:0)

欢迎使用Python。问题出在函数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)

我认为您现在可以看到您的代码实际上将self附加到floorEle1Skills;即,您附加主窗口的实例!因此,当您打印列表时,print语句会显示该列表包含一个对象。

答案 1 :(得分: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),以便将输入字符串值传递给处理按钮push的函数。

上面的代码打印到终端的用户输入(两次,首先是从传递的用户输入,第二次是列表中的所有项目)。

self.floorEle1Skills = []中放置showFrame()行会重置收集技能输入的列表(可以重新启动输入)。

上面的代码解决了问题中解决的两个问题,但这并不意味着没有其他问题需要解决。