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

2024-06-30 13:34:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我遇到的问题是当我向文本文件添加SkillStrength变量时(使用以下代码:myfile.write("Name: "+mEntry1.get()+", Age: "+mEntry2.get()+", Skill: "+str(x)+", Strength: "+str(v)))。x和v变量作为PY_VAR0和{}打印到.txt文档中。在

以下是我的完整代码:

///

#-------------------------------------------------------------------------------
# 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()
#-------------------------------------------------------------------------------
///

Tags: totexttxtageisplacebuttonrandom
1条回答
网友
1楼 · 发布于 2024-06-30 13:34:06

xvIntVar。您需要对它们中的每一个调用.get方法:

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

这样做将返回它们的值作为整数。在

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

同样,你可以用这样更好的方式写这行:

^{pr2}$

此方法使用^{},效率更高,更易于使用。在

相关问题 更多 >