所以这是我的代码,它基本上就像一个tkinter'app'作为计算器,时钟和游戏(更多细节将很快添加),但我想要做很多事情我目前的版本: EDIT 新版本,我可以将时间和游戏位打印到画布上 - 请回答代码答案!
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画布,而不是空闲shell。我该怎么做???
答案 0 :(得分:0)
有一些事情会给你带来麻烦。
op = raw_input('Enter exactly: *,/,+ or -, with quotation marks: ')
我不确定你为什么要求报价。如果它是一个字符串,那么你不需要它,因为raw_input给你输入的字符串。因为你的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语句重复一遍。