簡體   English   中英

使用python tkinter的問題

[英]Problems in using python tkinter

最初運行代碼時,閃爍將從行開始。 我的軟件應該做的是,如果用戶在最后一行的文本區域中輸入“ 1”,則閃爍應從列開始。

同樣,如果用戶輸入“ 1”,則應選擇字母並將其顯示在頂部文本區域中,並且整個過程應重新開始。當用戶在最后一行文本區域中輸入輸入時,我無法控制while循環。

我是python tkinter的初學者,我無法完全執行我想要的操作。

提前謝謝你

# your code goes here
import Tkinter
from  Tkinter import *
import tkMessageBox

top = Tkinter.Tk()

content=0

def helloCallBack1():
   tkMessageBox.showinfo( "Hello Python", "Hello World")

L1 = Label(top, text="Your Text Appears Here")
L1.grid(columnspan=10)
E1 = Entry(top, bd =5,width=40)
E1.grid(columnspan=10)

a1 = Tkinter.Button(top, text ="WATER",width="10", command = helloCallBack1)
a1.grid(row=4,column=0)
B = Tkinter.Button(top, text ="B", command = helloCallBack1)
B.grid(row=4,column=1)
C = Tkinter.Button(top, text ="C",command = helloCallBack1)
C.grid(row=4,column=2)
D = Tkinter.Button(top, text ="D", command = helloCallBack1)
D.grid(row=4,column=3)
E = Tkinter.Button(top, text ="E", command = helloCallBack1)
E.grid(row=4,column=4)
F = Tkinter.Button(top, text ="F", command = helloCallBack1)
F.grid(row=4,column=5)

row1 = Tkinter.Button(top, text =" ", command = helloCallBack1)
row1.grid(row=4,column=6)

a1 = Tkinter.Button(top, text ="ALARM",width="10",bg="red", command = helloCallBack1)
a1.grid(row=5,column=0)
H = Tkinter.Button(top, text ="H", command = helloCallBack1)
H.grid(row=5,column=1)
I = Tkinter.Button(top, text ="I", command = helloCallBack1)
I.grid(row=5,column=2)
J = Tkinter.Button(top, text ="J", command = helloCallBack1)
J.grid(row=5,column=3)
K = Tkinter.Button(top, text ="K", command = helloCallBack1)
K.grid(row=5,column=4)
L = Tkinter.Button(top, text ="L", command = helloCallBack1)
L.grid(row=5,column=5)

row2 = Tkinter.Button(top, text =" ", command = helloCallBack1)
row2.grid(row=5,column=6)

a1 = Tkinter.Button(top, text ="FOOD",width="10", command = helloCallBack1)
a1.grid(row=6,column=0)

N = Tkinter.Button(top, text ="N", command = helloCallBack1)
N.grid(row=6,column=1)
O = Tkinter.Button(top, text ="O",command = helloCallBack1)
O.grid(row=6,column=2)
P = Tkinter.Button(top, text ="P", command = helloCallBack1)
P.grid(row=6,column=3)
Q = Tkinter.Button(top, text ="Q",command = helloCallBack1)
Q.grid(row=6,column=4)
R = Tkinter.Button(top, text ="R", command = helloCallBack1)
R.grid(row=6,column=5)

row3 = Tkinter.Button(top, text =" ", command = helloCallBack1)
row3.grid(row=6,column=6)

a4 = Tkinter.Button(top, text ="BACKSPACE",width="10", command = helloCallBack1)
a4.grid(row=7,column=0)
S = Tkinter.Button(top, text ="S", command = helloCallBack1)
S.grid(row=7,column=1)
T = Tkinter.Button(top, text ="T", command = helloCallBack1)
T.grid(row=7,column=2)
U = Tkinter.Button(top, text ="U", command = helloCallBack1)
U.grid(row=7,column=3)
V = Tkinter.Button(top, text ="V", command = helloCallBack1)
V.grid(row=7,column=4)
W = Tkinter.Button(top, text ="W", command = helloCallBack1)
W.grid(row=7,column=5)


row4 = Tkinter.Button(top, text =" ", command = helloCallBack1)
row4.grid(row=7,column=6)

L2 = Label(top, text="Press 1 when you want to select")
L2.grid(columnspan=10)
E2 = Entry(top, bd =5,width=40)

E2.grid(columnspan=10)

content = E2.get()

content=0;

i=0;j=0;
while(i<30):

    row1.after(4000*j+1000*i, lambda: row1.config(fg="red",bg="black"))
    row1.after(4000*j+1000*(i+1), lambda: row1.config(fg="grey",bg=top["bg"]))
    row2.after(4000*j+1000*(i+1), lambda: row2.config(fg="red",bg="black"))
    row2.after(4000*j+1000*(i+2), lambda: row2.config(fg="grey",bg=top["bg"]))
    row3.after(4000*j+1000*(i+2), lambda: row3.config(fg="red",bg="black"))
    row3.after(4000*j+1000*(i+3), lambda: row3.config(fg="grey",bg=top["bg"]))

    row4.after(4000*j+1000*(i+3), lambda: row4.config(fg="red",bg="black"))
    row4.after(4000*j+1000*(i+4), lambda: row4.config(fg="grey",bg=top["bg"]))

    content=E2.get()
    if content==1:#this is not working
        break

    i=i+1
    j=j+1

top.mainloop()

問題是您的while循環像眨眼一樣運行,並且您同時無法輸入任何內容。 由於出現了after呼叫,因此閃爍持續存在,但這並不意味着您仍處於循環狀態。 當您在框中輸入內容時,程序退出該循環很長時間。

我要做的是將輸入框綁定到某個鍵(例如Return),並在按下該鍵時檢查輸入框的內容,如果為1,則停止閃爍。

您也可以將整個東西綁定到1鍵上,而避免整個Entry小部件東西

讓我們考慮一下您要實現的目標:您試圖循環瀏覽對象列表,一次“閃爍”一個對象。 您要首先對對象一次執行一次,然后對對象一次執行一次。

我假設這兩種行為之間唯一的不同是要迭代哪些項目。 因此,如果創建通用功能以通過對象列表順序“閃爍”,則可以簡單地切換正在“閃爍”的對象。

這將如何運作? 讓我們從創建要“閃爍”的事物列表開始:

blink_objects = [row1, row2, row3, row4]

想法是我們希望一次“閃爍”這些。 要更改閃爍的內容(如您的問題所示,從行切換為列),您只需要重新定義blink_objects

我們將如何眨眼? 做這種動畫的通常方法是創建一個繪制一幀動畫的函數,然后安排自己在短時間后再次運行。 在您的情況下,該時間段是一秒鍾。

我們稱此功能為blink 我們希望它帶有幾個可選的args,稍后將使用。 第一個是一個變量,其中包含當前處於“閃爍”狀態的對象。 我們需要它,以便我們可以將其改回。 第二個是一個標志,可用於將來停止動畫。

該函數的外觀如下。 current_object參數在內部通過blink傳遞,並且從其他地方調用blink時不應設置該參數。

def blink(current_object = None, stop=False):
    global blink_objects

    # "unblink" the current object
    if current_object:
        current_object.configure(fg="black", bg=top["bg"])
        current_object = None

    if not stop:
        # blink the first item in the list of objects,
        # then move the object to the end of the list
        current_object = blink_objects.pop(0)
        blink_objects.append(current_object)
        current_object.configure(bg="black", fg="red")

        # schedule the blink again after a second
        current_object.after(1000, blink, current_object)

您只需要調用一次此函數。 它將每秒鍾再次調用一次,直到時間結束。 為了使它像您的原始程序一樣閃爍,我們只需要用以下兩行代碼來替換整個while循環:

blink_objects = [row1, row2, row3, row4]
blink()

如果您想隨時停止動畫,則可以調用blink(stop=True) 例如,當用戶退出程序時,您可能想要這樣做。

接下來,我們需要進行設置,以便鍵入“ i”可以更改閃爍的內容。 我們可以通過設置特定的綁定來做到這一點,該綁定將在用戶按下鍵時立即觸發:

E2.bind("<i>", change_blink_objects)

這就是“如果用戶按下'i',則調用change_blink_objects”。 現在我們只需要定義change_blink_objects

由於此函數是從綁定中調用的,因此將向該對象傳遞一個表示事件的對象(按下了什么鍵,哪個對象得到了按鍵等等)。 目前,我們不需要這些信息,但是我們必須接受它。

在該函數中,我猜想它會智能地選擇要閃爍的對象,但是我不知道該邏輯是什么,所以我只是讓它在“警報”行中閃爍。

def change_blink_objects(event):
    global blink_objects
    blink_objects = [H, I, J, K,. L)

這就是您要做的全部工作:創建用於閃爍對象列表的通用函數,以及一個綁定,該綁定調用用於更改對象列表的函數。

確保在測試時先單擊條目小部件,否則在鍵入“ i”時不會看到它。

雖然循環和GUI不能很好地融合在一起。 Youe while循環會創建240個無法取消的延遲回調。 相反,您應該有一個延遲的回調,該回調有條件地創建另一個回調。 這是未經測試的循環替代品。 它結合了Gabor的答案,應該可以幫助您入門,

go = True
def stop():
    go = False
root.bind('<key-1>', stop)

def blink(i, j):
    if i = 0:
        row1.config(fg="red",bg="black"))
        if j > 0:
            row4.config(fg="grey",bg=top["bg"]))
    elif i = 1:
        row1.config(fg="grey",bg=top["bg"]))
        row2.config(fg="red",bg="black"))
    elif i = 2:
        row2.config(fg="grey",bg=top["bg"]))
        row3.config(fg="red",bg="black"))
    elif i = 3:
        row3.config(fg="grey",bg=top["bg"]))
        row4.config(fg="red",bg="black"))
    if go and j < 30:
        top.after(1000, blink, (i+1) % 4, j+1)

top.after(1000, blink, 0, 0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM