当前位置: 代码迷 >> python >> 为什么不起作用? (tkinter,python和函数)
  详细解决方案

为什么不起作用? (tkinter,python和函数)

热度:70   发布时间:2023-06-16 10:09:02.0

所以这是我的代码,基本上就像tkinter的“ app”一样,可以用作计算器,时钟和玩游戏(更多细节将很快添加),但是当前版本有很多事情我想做: 编辑新版本,我可以在画布上打印时间和游戏位吗?-请回答答案!

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画布上,而不是空闲的外壳上。 我该怎么做呢???

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

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

我不确定您为什么要报价。 如果要使其成为字符串,则不需要它,因为raw_input会为您提供键入为字符串的内容。 并且因为您的if语句都没有检查if op == '*':带引号的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语句重复该操作。

  相关解决方案