Python同时运行2个无限while循环

时间:2019-05-24 15:25:54

标签: python-3.x tkinter python-multiprocessing

我正在尝试创建一个实质上是cookie单击器的程序。如何使tkinter窗口一方面不断更新,另一方面又每秒更新一次?

这是针对半新的python用户的,我使用的是学校发行的MacBook Air。我曾尝试使用多处理,光线和线程,但是我所看到的关于它们的一切使我感到困惑。我尝试使用嵌套的while循环,但是当我需要在按键时以及每秒更新一次时,窗口仅每秒更新一次。

import random, time, threading, multiprocessing
click_number = 400
tap_num = 0
tap_num = 0
event1 = 0
event2 = 0
event3 = 0
event4 = 0

def click_button():
    global click_number, event1, event2, event3, event4
    click_number += 1
    click_number_label.config(text=click_number)
    if click_number >= 500 and event4 == 0:
        root.title("Alright, Now That You Have Gotten This Many Clicks I'll Show You How To Use Them")
        canvas.config(width=1260,height=768)
        click_number_label.place(x=random.randint(0,1260),y=random.randint(0,768))
        notification_label.place_forget()
        buy_button.grid(column=2,row=0)
        root.attributes('-fullscreen',True)
        event4 = 1
    elif click_number >= 500 and event4 != 0:
        click_number_label.place(x=random.randint(0,1260),y=random.randint(0,768))
    elif click_number >= 200 and event3 == 0:
        root.title("Here, I'll Give You Something To Make This A Little Easier")
        canvas.bind("<Key>",tap_button)
        notification_label.place(x=400,y=100)
        event3 = 1
    elif click_number >= 100 and event2 == 0:
        root.title("You're Still Going At It Huh?")
        event2 = 1
    elif click_number >= 50 and event1 == 0:
        root.title("Wow, That's A Lot Of Clicks")
        event1 = 1
    root.update()

def tap_button(event):
    global click_number, event1, event2, event3, event4
    click_number += 1
    click_number_label.config(text=click_number)
    if click_number >= 500 and event4 == 0:
        root.title("Alright, Now That You Have Gotten This Many Clicks I'll Show You How To Use Them")   
        canvas.config(width=1260,height=768)
        click_number_label.place(x=1260/2,y=330)
        notification_label.place_forget()
        buy_button.grid(column=2,row=0)
        root.attributes('-fullscreen',True)
        event4 = 1
    elif click_number >= 500 and event4 != 0:
        click_number_label.place(x=1260/2,y=330)
    elif click_number >= 200 and event3 == 0:
        root.title("Here, I'll Give You Something To Make This A Little Easier")
        canvas.bind("<Key>",tap_button)
        notification_label.place(x=400,y=100)
        event4 = 1
    root.update()

def add_tapper():
    global tap_num
    global click_number
    if click_number >= 100:
        tap_num += 1
        click_number -= 100
        tap_num_label.config(text=tap_num)
        click_number_label.config(text=click_number)
        root.update()

click_number_label = Label(root,text=click_number,justify=CENTER)
click_number_label.place(x=380,y=330)

root = Tk()
root.configure(background='#40005c')

canvas = Canvas(root,width=800, height=600,background='#40005c',highlightbackground='#4a005c')
canvas.grid(column=1,row=0)
canvas.focus_set()

yag = PhotoImage(file="table.ppm")
click_button = Button(root,command=click_button)
click_button.config(image=yag,width=300,height=300)
click_button.grid(column=1,row=0)

buy_button = Button(root,text='More Clicks',command=add_tapper)
tap_num_label = Label(root,text=tap_num)
tap_num_label.place(x=500,y=500)

def loop():
    while True:
        root.update()

def passive():
    while True:
        if tap_num >= 1:
            click_number = click_number + tap_num
            click_number_label.config(text=click_number)
            root.update()
            time.sleep(1)

if __name__ == '__main__':
    t1 = multiprocessing.Process(target=loop)
    t2 = multiprocessing.Process(target=passive)

t1.start()
t2.start()

我希望它既运行不断循环更新窗口的循环,又运行另一个循环,以便它将更新您单击按钮的次数而不是每秒一次。相反,我收到此错误:

Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.

我不知道此错误意味着什么,所以我不知道如何解决。

0 个答案:

没有答案