如何将tkinter文本框的内容保存为字符串变量?

huangapple go评论69阅读模式
英文:

How to save tkinter textbox content as a string variable?

问题

我遇到了学习如何将 tkinter 中的文本框输入保存为字符串变量的问题。以下是我迄今为止的代码。

import tkinter as tk
from tkinter import messagebox

class Setup:

    def __init__(self):
        
        # 设置窗口布局
        self.root = tk.Tk()
        self.root.geometry("500x300")
        self.root.title("Setup")

        self.label = tk.Label(self.root, text="Title of next window", font=('Arial', 18))
        self.label.pack(padx=10, pady=10)

        self.textbox = tk.Text(self.root, height=1, font=('Arial', 16))
        self.textbox.pack(padx=50, pady=10)

        self.button = tk.Button(self.root, text="Save", font=('Arial', 18), command=self.save)
        self.button.pack(padx=10, pady=10)

        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

        self.root.mainloop()

    def on_closing(self):
        if messagebox.askyesno(title="Quit?", message="Do you really want to quit?"):
            self.root.destroy()

    # 将文本框内容保存为变量 title 并打开计数器窗口
    def save(self):
        title = self.textbox.get('1.0', tk.END)
        Counter(title)

class Counter:
    def __init__(self, title):
        
        # 设置计数器窗口布局
        self.root = tk.Tk()
        self.root.geometry("500x300")
        self.root.title(title) 

Setup()

我尝试让文本框的内容成为计数器窗口的标题。有谁能帮忙吗?

英文:

I'm having trouble learning how to save a textbox input in tkinter as a string variable. This is what I have so far.

from tkinter import messagebox

class Setup:

    def __init__(self):
        
        #layout of setup window
        self.root = tk.Tk()
        self.root.geometry("500x300")
        self.root.title("Setup")

        self.label = tk.Label(self.root, text="Title of next window", font=('Arial', 18))
        self.label.pack(padx=10, pady=10)

        self.textbox = tk.Text(self.root, height=1, font=('Arial', 16))
        self.textbox.pack(padx=50, pady=10)

        self.button = tk.Button(self.root, text="Save", font=('Arial', 18), command=self.save)
        self.button.pack(padx=10, pady=10)

        self.root.protocol("WM_DELETE_WINDOW", self.on_closing)

        self.root.mainloop()

    def on_closing(self):
        if messagebox.askyesno(title="Quit?", message="Do you really want to quit?"):
            self.root.destroy()

    #save the textbox as var title and open counter
    def save(self):
        title = self.textbox.get('1.0', tk.END)
        Counter()

class Counter:
    def __init__(self):
        
        #layout of counter window
        self.root = tk.Tk()
        self.root.geometry("500x300")
        self.root.title(title) 

Setup()

I'm trying to have the textbox contents be the title of the counter window. Can anyone help with this?

答案1

得分: 2

你已经将它存储在一个变量中。当你使用它时,该变量处于 作用域之外。所以使用一个 全局变量

    def save(self):
        global title
        title = self.textbox.get('1.0', tk.END)
        Counter()

请参考你的书中关于作用域的章节或这里

英文:

You are already storing it in a variable. That variable is out of scope when you use it. So use a global variable:

    def save(self):
        global title
        title = self.textbox.get('1.0', tk.END)
        Counter()

Refer to the chapter on scope in your book or here

答案2

得分: 2

从您提供的内容中,以下是翻译好的部分:

"Never used global.
The correct way is to add data in __init__ parameter
Snippet:

def save(self):
    
    title = self.textbox.get('1.0', tk.END)
    Counter(title)

class Counter:
    def __init__(self, data):
        
        #layout of counter window
        self.root = tk.Tk()
        self.root.geometry("500x300")
        self.root.title(data) 

Screenshot:

如何将tkinter文本框的内容保存为字符串变量?

英文:

Never used global.
The correct way is to add data in __init__ parameter

Snippet:

    def save(self):
        
        title = self.textbox.get('1.0', tk.END)
        Counter(title)

class Counter:
    def __init__(self, data):
        
        #layout of counter window
        self.root = tk.Tk()
        self.root.geometry("500x300")
        self.root.title(data) 

Screenshot:

如何将tkinter文本框的内容保存为字符串变量?

答案3

得分: 0

你可以在你的Setup类的开头定义变量self.title,然后使用它来获取文本框的内容:

def save(self):
    self.title = self.textbox.get("1.0", tk.END)
    Counter()
英文:

You could define the variable self.title at the start of your Setup class, and then use this to get your textbox's content:

def save(self):
     self.title = self.textbox.get("1.0", tk.END)
     Counter()

huangapple
  • 本文由 发表于 2023年3月7日 02:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定