0

私は GCSE のためにコンピューティング コースを受講しており、私のタスクの 1 つは「キャラクター アトリビュート クリエーター」を作成することでした。私は基本的に、.txt ファイルを使用して Tkinter で作成し、キャラクターを作成し、名前、年齢、スキル、および強さを表示しました。

Skill私が遭遇する問題は、変数とStrength変数をテキスト ファイルに追加するときです (次myfile.write("Name: "+mEntry1.get()+", Age: "+mEntry2.get()+", Skill: "+str(x)+", Strength: "+str(v))のコードを使用します: .x 変数と v 変数は、.txt ドキュメントにPY_VAR0andとして出力され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

xvですIntVar.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 に答える