为什么这行不通? (tkinter,python 和函数)

Why won't this work? (tkinter, python and functions)

所以这是我的代码,它基本上就像一个 tkinter 'app' 作为计算器、时钟和玩游戏(更多细节将很快添加),但我有很多东西想用我当前的版本: 编辑 新版本,我可以将时间和游戏位打印到 canvas-- 请回答代码回答!

from Tkinter import *
from sys import *
from datetime import *
import time

print 'LOADING'
toolbar_width = 40


for i in range(toolbar_width):
    time.sleep(0.1) # do real work here
    # update the bar
    sys.stdout.write("-")
    sys.stdout.flush()

sys.stdout.write("\n")


sys.stdout.write("\n")

root = Tk()
root.iconbitmap(r'C:\Users\Home Laptop\Downloads\favicon.ico')
root.wm_title("SimpleOS")

p = Text(root, height = 2, width = 30) 
p.pack()


def time_app():
    t = raw_input('Would you like the time in hh:mm:ss, dd.mm.yy, or both (Type either h for the first option, d for the second, and b for the third): ')
    now=datetime.now()
    cy = now.year
    cm = now.month
    cd = now.day
    ch = now.hour
    cm1 = now.minute
    cs = now.second
    if t == 'h':
        print '%s:%s:%s' %(ch,cm1,cs)
        sys.exit()
        time.sleep(3)
    elif t == 'd':
        print '%s.%s.%s' %(cd,cm,cy)
        time.sleep(3)
        sys.exit()
    elif t == 'b':
        print '%s.%s.%s/%s:%s:%s' %(cd,cm,cy,ch,cm1,cs)
        time.sleep(3)
        sys.exit()
    else:
        sys.exit()


def calculate():
    i = float(raw_input('Enter your first number: '))
    ii = float(raw_input('Enter your second number: '))
    op = raw_input('Enter exactly: *,/,+ or -, with no quotation marks: ')
    ord1 = raw_input("To do the calculation in the order: i ---> ii, type 'a', to do it in this order: ii ---> i, type 'b', both with no quotes: ")
    if ord1 == 'a':
        if op == '*':
            value = str(i*ii)
            p.delete(1.0, END)
            p.insert(END, value)
        elif op == '/':
            value = str(i/ii)
            p.delete(1.0, END)
            p.insert(END, value)
        elif op == '+':
            value = str(i+ii)
            p.delete(1.0, END)
            p.insert(END, value)
        elif op == '-':
            value = str(i-ii)
            p.delete(1.0, END)
            p.insert(END, value)
        else:
            print 'UNEXPECTED ERROR'
            sys.exit()
    elif ord1 == 'b':
        if op == '*':
            value = str(ii*i)
            p.delete(1.0, END)
            p.insert(END, value)
        elif op == '/':
            value = str(ii/i)
            p.delete(1.0, END)
            p.insert(END, value)
        elif op == '+':
            value = str(ii+i)
            p.delete(1.0, END)
            p.insert(END, value)
        elif op == '-':
            value = str(ii-i)
            p.delete(1.0, END)
            p.insert(END, value)


def RPG():
    charChoice = raw_input('Would you like to be a ')    


app1 = ("Calculator")
app2 = ("Clock")
app3 = ("RPG")
w = Button(root, text="Calculator",command=calculate )
x = Button(root, text="Clock", command=time_app )
y = Button(root, text="RPG", command=RPG)

w.pack(side=LEFT)
x.pack(side=LEFT)
y.pack(side=LEFT)

root.mainloop()
time.sleep(5)
sys.exit()

我想了解如何将输入打印到 tkinter canvas,而不是闲置 shell。我该怎么做???

有几件事给您带来了麻烦。

op = raw_input('Enter exactly: *,/,+ or -, with quotation marks: ')

我不确定你为什么要报价。如果要使它成为一个字符串,那么您不需要它,因为 raw_input 为您提供了作为字符串键入的内容。并且因为 none 的 if 语句正在检查带引号的操作 if op == '*': 你总是会遇到意外错误。

现在要写入您的文本小部件,您只需用这些行替换您的印刷品

p.delete(1.0, END) # Removes all text already in widget
p.insert(END, mytext) # Insert your text where the end of the cursor is
                      # This does not need to be a string

我建议将打印行更改为 value = i*ii 然后在你的 if 语句之外有

p.delete(1.0, END)
p.insert(END, value)

这样您就不需要为每个 if 语句重复该操作。