簡體   English   中英

為什么在打印時使用python中的tkinter將輸入表示為對象?

[英]why is the input when printed represented as an object using tkinter in python?

我正在編寫一個程序,該程序將從文本條目中輸入技能名稱作為輸入,並計算輸入的所有技能的相應值。 當我在程序中輸入技能然后將技能打印到外殼時,它會顯示為對象嗎? 為什么會發生這種情況,我該如何解決?是否需要reprstr 為什么刪除文本輸入的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。 問題出在函數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語句顯示該列表包含一個對象。

正如在另一個答案中已經提到的那樣,代碼的問題圍繞着函數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()重置收集技能輸入的列表(使輸入可能重新啟動)。

上面的代碼解決了問題中解決的兩個問題,但這並不意味着沒有其他問題需要解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM