Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在python中使用tkinter将打印时的输入表示为对象?_Python_Python 3.x_Tkinter_Representation_Tkinter Entry - Fatal编程技术网

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

为什么在python中使用tkinter将打印时的输入表示为对象?,python,python-3.x,tkinter,representation,tkinter-entry,Python,Python 3.x,Tkinter,Representation,Tkinter Entry,我正在编写一个程序,将技能名称作为文本输入,并计算输入的所有技能的相应值。当我在程序中输入一项技能,然后将该技能打印到shell时,它会显示为一个对象吗?为什么会发生这种情况?我如何修复它?我需要repr还是str?为什么清除文本输入的delete方法也不起作用 import tkinter as tk from tkinter import ttk #make the lists to store the skill names floorEle1Skills = [] class sta

我正在编写一个程序,将技能名称作为文本输入,并计算输入的所有技能的相应值。当我在程序中输入一项技能,然后将该技能打印到shell时,它会显示为一个对象吗?为什么会发生这种情况?我如何修复它?我需要repr还是str?为什么清除文本输入的delete方法也不起作用

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

欢迎使用Python。问题出在函数
1(技能)
中。这是
类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
附加到FloorRele1Skills;i、 例如,添加主窗口的实例!因此,当您打印列表时,print语句显示列表包含一个对象。

正如在另一个答案中已经提到的,代码的问题使函数
floorel1(self,skill)
,但是。。。还有一些其他问题需要妥善解决,以便将输入的技能传递到技能列表中(参见下面的代码):

守则的其他修订如下:

在“\uuuuu_uuuu uuuu()”函数中定义
self.floorelSkills=[]
,并将适当的参数传递给
controller.floorel1(ele1Entry)
,以便将输入字符串值传递给处理按钮按下的函数

上面的代码将用户输入打印到终端(两次,第一次来自传递的用户输入,第二次来自列表中的所有项目)

self.floorelSkills=[]
行放入
showFrame()
重置收集技能输入的列表(使输入重新启动成为可能)


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

你试过我回答的密码了吗?它解决了你在问题中提到的问题,对吗?
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()