尝试将下拉菜单中的输入返回到Tkin中的变量

2024-10-01 13:24:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写我的第一个程序,我不知道如何解决这个问题。我一周前就开始学习Python了,对Tkinter和OOP还是很陌生的。我让这个脚本(D&D NPC Generator)在没有使用OOP的情况下工作,但是我想看看如何使用OOP来实现它,因为大多数人似乎都喜欢使用Tkinter。在

以下是输入窗口的代码:

class Input:

    def __init__(self, master):

        ideals = ["Good", "Evil",
                  "Lawful", "Chaotic",
                  "Neutral", "Other"]

        ideal_selection = StringVar()
        ideal_selection.set(ideals[0])

        self.name_label = Label(master, text="NPC Name: ")
        self.name_entry = Entry(master)

        self.ideal_label = Label(master, text="Ideal Type: ")
        self.ideal_entry = OptionMenu(master, ideal_selection, *ideals)

        self.submit_button = Button(text="Submit", command=self.close_window)

        self.name_label.grid(row=0, column=0)
        self.name_entry.grid(row=0, column=1)

        self.ideal_label.grid(row=1, column=0)
        self.submit_button.grid(columnspan=2, row=3, column=0)

        self.ideal_entry.grid(row=1, column=1)

    def close_window(self):
        global name
        global ideal_type
        name = self.name_entry.get()
        ideal_type = self.ideal_selection.get()
        self.master.destroy()

它又回来了:

^{pr2}$

我不知道出什么事了。我使用这个GUI窗口的目的是让用户输入NPC的名称,然后从drown菜单中选择一个选项,以确定用户希望NPC拥有什么样的理想。对我做错的地方进行纠正和解释会很有帮助的,谢谢。在


Tags: textnameselfmastertkintercolumnlabeloop
1条回答
网友
1楼 · 发布于 2024-10-01 13:24:54

您已将ideal_selection声明为局部变量,而不是类实例变量。 因此,调用self.ideal_selection.get()将失败,因为没有要引用的self。在

您需要将声明从:ideal_selection = StringVar()更改为:this.ideal_selection = StringVar(),并将所有其他引用更改为this.ideal_selection。在

请注意,您已经为其他所有内容(self.name_entry)执行了此操作。。。在

后记:

我不鼓励你在这里使用global。当您在OOP原则下运行tkinter时,您可以将类中的值返回给调用脚本。在

(请注意,最后我建议不要使用from tkinter import *)。在

看看如果您的代码修改为:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import tkinter as tk
import sys

class Input(tk.Frame):

    def __init__(self, parent):

        tk.Frame.__init__(self, parent)
        self.parent = parent

        ideals = ["Good", "Evil",
                  "Lawful", "Chaotic",
                  "Neutral", "Other"]

        self.ideal_selection = tk.StringVar()
        self.ideal_selection.set(ideals[0])

        self.name_label = tk.Label(root, text="NPC Name: ")
        self.name_entry = tk.Entry(root)

        self.ideal_label = tk.Label(root, text="Ideal Type: ")
        self.ideal_entry = tk.OptionMenu(root, self.ideal_selection, *ideals)

        self.submit_button = tk.Button(text="Submit", command=self.close_window)

        self.name_label.grid(row=0, column=0)
        self.name_entry.grid(row=0, column=1)

        self.ideal_label.grid(row=1, column=0)
        self.submit_button.grid(columnspan=2, row=3, column=0)

        self.ideal_entry.grid(row=1, column=1)

    def close_window(self):
        #global name
        #global ideal_type
        self.name = self.name_entry.get()
        self.ideal_type = self.ideal_selection.get()
        #self.destroy()
        self.quit()

if __name__ == '__main__':
    root = tk.Tk()
    root.geometry("600x400+300+300")
    app = Input(root)
    root.mainloop()
    # Note the returned variables here
    # They must be assigned to external variables
    # for continued use
    returned_name = app.name
    returned_ideal = app.ideal_type
    print("Your name is: " + returned_name)
    print("Your ideal is: " + returned_ideal)
    # Should only need root.destroy() to close down tkinter
    # But need to handle user cancelling the form instead
    try:
        root.destroy()
    except:
        sys.exit(1)  

相关问题 更多 >