0

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

我遇到的问题是当我将SkillandStrength变量添加到文本文件时(使用此代码:myfile.write("Name: "+mEntry1.get()+", Age: "+mEntry2.get()+", Skill: "+str(x)+", Strength: "+str(v)). x 和 v 变量作为PY_VAR0and打印到 .txt 文档中PY_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()
#-------------------------------------------------------------------------------
///
4

1 回答 1

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更有效且更易于使用。

于 2013-11-02T01:00:35.287 回答