Tkinter背景输入

2024-09-27 01:23:34 发布

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

我有一个简单的程序,它通过raw_input函数获取两个字符串并对它们进行比较。它通过在shell中打印来告诉我它们是否相同。在

但是,我想给这个简单的程序添加一个接口。我希望结果显示在窗户上,而不是外壳上。我使用了Tkinter,但问题是只要我运行程序,Tkinter窗口就会弹出(这是应该的),但现在我无法输入shell。我必须点击外壳,然后我才能打字。在

我想让我的代码与条形码扫描仪一起工作,所以我必须单击外壳,然后开始扫描。简单的版本确实有效。但是有了Tkinter界面,我首先要点击外壳,然后只有我可以扫描。在

有办法吗?我希望能够运行设置,而不做任何事,开始扫描。在

这是我的代码:

from Tkinter import *
import sys

font = "Helvetica"

wnd = Tk()
wnd.title("Barcode Checker")
c = Canvas(wnd, width=800,height=600,background="grey")

green = PhotoImage(file = "green.gif")
blue = PhotoImage(file = "blue.gif")
red = PhotoImage(file = "red.gif")
yellow = PhotoImage(file = "yellow.gif")

#def keypress(e):
#    if e.keysym == "Escape":
#        wnd.destroy()

#wnd.bind_all("<Key>",keypress)

c.create_image(400,300,image = blue)

barcode = ''      

c.create_text(400,200, text = "Please scan the barcode\n you want to save", font =   (font,40))
c.pack()

def keypress(e):
    if e.keysym== "Escape":
        wnd.destroy()
        sys.exit()
wnd.bind_all("<Key>",keypress)


barcode = raw_input("Scan the MAIN item: ")

if barcode != '':
    c.create_text(400,320, text = "Barcode successfully scanned.",font = (font,35))
    c.create_image(400,300,image = yellow)
    c.create_text(400,350, text = "Barcode you have scanned: "+str(barcode),font = (font,30))
    c.create_text(400,200,text = "Please begin scanning.", font=(font,40))
    print "Barcode successfully scanned"

while True:
    barcode2 = raw_input("Scan the item you want to check: ")

    if barcode2== "quit":
        break

    if barcode2 == barcode:
        c.create_image(400,300,image = green)
        c.create_text(400,100, text = "Main Barcode: "+ str(barcode),font=(font,30))
        c.create_text(400,300, text = "New Barcode: "+ str(barcode2),font=(font,30))
        c.create_text(400,350, text = "The barcodes are identical", font=(font, 35))
        print "The barcodes are identical"

    if barcode2 != barcode:
        c.create_image(400,300,image = red)
        c.create_text(400,100, text = "Main Barcode: "+str(barcode),font=(font,30))
        c.create_text(400,300, text= "New Barcode: "+str(barcode2),font=(font,30))
        c.create_text(400,350, text = "The barcodes do not match", font=(font, 35))
        print "The barcode does NOT match"


c.pack()
wnd.destroy()
sys.exit()
wnd.mainloop()

谢谢。在


Tags: textimageiftkintercreate外壳gifbarcode
1条回答
网友
1楼 · 发布于 2024-09-27 01:23:34

你有一些选择:

1-完整的Tkinter接口。使用tkMessagebox.showinfo(title,message) 显示结果和tkSimpleDialog.askstring(title,message,initialvalue="",parent=None)

2-为响应创建函数。使用以下功能:

def response():
    wnd = Tk()
    wnd.title("Barcode Checker")
    c = Canvas(wnd, width=800,height=600,background="grey")

    green = PhotoImage(file = "green.gif")
    blue = PhotoImage(file = "blue.gif")
    red = PhotoImage(file = "red.gif")
    yellow = PhotoImage(file = "yellow.gif")

    c.create_image(400,300,image = blue)
    c.create_text(400,200, text = "Please scan the barcode\n you want to save", font =   (font,40))
    c.pack()

    c.create_text(400,320, text = "Barcode successfully scanned.",font = (font,35))
    c.create_image(400,300,image = yellow)
    c.create_text(400,350, text = "Barcode you have scanned: "+str(barcode),font = (font,30))
    c.create_text(400,200,text = "Please begin scanning.", font=(font,40))
    print "Barcode successfully scanned"

    if barcode2 == barcode:
        c.create_image(400,300,image = green)
        c.create_text(400,100, text = "Main Barcode: "+ str(barcode),font=(font,30))
        c.create_text(400,300, text = "New Barcode: "+ str(barcode2),font=(font,30))
        c.create_text(400,350, text = "The barcodes are identical", font=(font, 35))
        print "The barcodes are identical"

    if barcode2 != barcode:
        c.create_image(400,300,image = red)
        c.create_text(400,100, text = "Main Barcode: "+str(barcode),font=(font,30))
        c.create_text(400,300, text= "New Barcode: "+str(barcode2),font=(font,30))
        c.create_text(400,350, text = "The barcodes do not match", font=(font, 35))
        print "The barcode does NOT match"

    c.pack()
    wnd.bind_all("<Key>", keypress)
    wnd.mainloop()

相关问题 更多 >

    热门问题