如何在 python 3.x tkinter 中保存文本形式的文本框(String 和 Int)

How to save text form a textbox (String and Int) in python 3.x tkinter

我正在尝试从 6 个文本框中保存一些名称和值,其中 2 个是字符串,4 个是整数,在 .txt 或 .doc 中,但它不起作用

我在 windows 8.1 中使用 python 3.4,有人可以帮助我吗,我无法在 python 3.4 中找到任何信息或示例来保存我写的任何内容在这 6 个文本框中 我有一个调用函数 mSave

的按钮

我有以下代码要保存,但无法正常工作

def mSave():
    filename = asksaveasfilename(defaultextension='.txt',
                                 filetypes=(('Text files', '*.txt'),
                                            ('Python files', '*.py *.pyw'),
                                            ('All files', '*.*')))
  if filename:
     with open(filename, 'w') as stream:
          stream.write(self.gettext())

Python 说

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\idlelib\run.py", line 121, in main
    seq, request = rpc.request_queue.get(block=True, timeout=0.05)
  File "C:\Python34\lib\queue.py", line 175, in get
    raise Empty
queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\ManualTab.py", line 608, in mSave
    filename = asksaveasfilename(defaultextension='.txt',
NameError: name 'asksaveasfilename' is not defined

请帮助

我找到了答案

好的,首先,对于python 3.4,我必须使用

from tkinter import *

但这并不意味着它将全部导入所以我必须调用

from tkinter import filedialog
from tkinter.filedialog import asksaveasfilename

如果有人需要,open也是一样的

from tkinter.filedialog import askopenfilename

现在我终于可以调用我的函数了

def mSave():
  filename = asksaveasfilename(defaultextension='.txt',filetypes = (('Text files', '*.txt'),('Python files', '*.py *.pyw'),('All files', '*.*')))
  if filename is None:
    return
  file = open (filename, mode = 'w')
  name1 = TubeLenghtVal_1.get()          
  name2 = TubeLenghtVal_2.get()

  # or whatever you assign the name of the variable, for me is TubeLenghtVal_2 = StringVar() and for integer I have Vol_Val = IntVar()
  all = name1 + " , " + name + " , " + (str(Vol_Val))
  file.write(all)
  file.close()

仅此而已