英文:
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:
英文:
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:
答案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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论