整数在.txt文档中显示为PY_VAR0

时间:2013-11-01 22:48:17

标签: python tkinter

对于我的GCSE,我参加了计算课程,其中一项任务是制作一个“角色属性创建者”。我基本上在Tkinter中使用.txt文件创建了一个字符,并显示了一个名字,年龄,技能和力量。

我遇到的问题是我将SkillStrength变量添加到文本文件中(使用此代码:myfile.write("Name: "+mEntry1.get()+", Age: "+mEntry2.get()+", Skill: "+str(x)+", Strength: "+str(v))。x和v变量打印到.txt文档为PY_VAR0PY_VAR1

这是我的完整代码:

///

#-------------------------------------------------------------------------------
# Import
from Tkinter import *
import tkMessageBox
import random
#-------------------------------------------------------------------------------
# Program
mGui = Tk()
mGui.geometry("300x300")
mGui.title("Character Attribrute Setter")
#-------------------------------------------------------------------------------
# Definitions...
x = IntVar()
v = IntVar()
x.set(0)
v.set(0)
# Decides Skill... random.randint()
def mStren():
    y = random.randint(1,12)
    c = random.randint(1,4)
    v = y/c
# Decides Strength... random.randint()
def mSkill():
    a = random.randint(1,12)
    b = random.randint(1,4)
    x = a/b
# How it Works... tkMessageBox() (mMb1)
def mMB1():
    mMB1 = tkMessageBox.showinfo(title="How it works",message="PLEASE ENTER ALL                  INFORMATION AND PRESS BOTH SIMULATE BUTTONS! The aim of this program is to determine a characters Strength and Skill. It does this by setting two Integers to 10. After that, a 12 sided die is rolled, after that, a 4 sided die is rolled. The outcome of the 12 sided die is divided by the outcome of the 4 sided die. This total is then added to the Integers, thus determining Skill and Strength.")
# Create... Save to a .txt file (mC1)
def mC1():
    myfile = open("Character.txt", "w")
    myfile.write("Name: "+mEntry1.get()+", Age: "+mEntry2.get()+", Skill: "+str(x)+", Strength: "+str(v))
    myfile.close()
#-------------------------------------------------------------------------------
# Welcome... Label (mLabel1)
mLabel1 = Label(text="Welcome to: Character Attribute Setter!")
mLabel1.pack()
#-------------------------------------------------------------------------------
# Created By... Label (mLabel2)
mLabel2 = Label(text="Created By: Harry Spicer.")
mLabel2.pack()
#-------------------------------------------------------------------------------
# How it works... Button (mButton1)
mButton1 = Button(text="How it works",command=mMB1)
mButton1.place(x=0,y=275)
#-------------------------------------------------------------------------------
# Please enter your name... Label (mlabel3)
mLabel3 = Label(text="Please enter your name:")
mLabel3.place(x=0,y=75)
#-------------------------------------------------------------------------------
# Please enter your Age... Label (mlabel4)
mLabel3 = Label(text="Please enter your Age:")
mLabel3.place(x=0,y=175)
#-------------------------------------------------------------------------------
# Name... Entry (mEntry1)
mEntry1 = Entry()
mEntry1.place(x=0,y=100)
#-------------------------------------------------------------------------------
# Age... Entry (mEntry1)
mEntry2 = Entry()
mEntry2.place(x=0,y=200)
#-------------------------------------------------------------------------------
# Create... Button (mButton2)
mButton2 = Button(text="Create Character",command=mC1)
mButton2.pack()
#-------------------------------------------------------------------------------
# Simulate Skill... Button (mButton3)
mButton3 = Button(text="Simulate Skill",command=mSkill)
mButton3.pack()
#-------------------------------------------------------------------------------
# Simulate Skill... Button (mButton4)
mButton4 = Button(text="Simulate Strength",command=mStren)
mButton4.pack()
#-------------------------------------------------------------------------------
# Mainloop
mGui.mainloop()
#-------------------------------------------------------------------------------
///

1 个答案:

答案 0 :(得分:1)

xvIntVar。您需要在每个方法上调用.get方法:

myfile.write("Name: "+mEntry1.get()+", Age: "+mEntry2.get()+", Skill: "+str(x.get())+", Strength: "+str(v.get()))

这样做会将其值作为整数返回。

如果没有这个,你所拥有的只是对变量本身的引用,而不是它们的值。

此外,你可以用更好的方式写出这一行:

myfile.write("Name: {}, Age: {}, Skill: {}, Strength: {}".format(mEntry1.get(), mEntry2.get(), x.get(), v.get()))

此方法使用str.format,效率更高,更易于使用。