while 循环使我的 customtkinter 应用程序冻结。我怎样才能做一个不会冻结应用程序的更好的功能?

问题描述 投票:0回答:3

我正在使用 customtkinter 库制作游戏 该游戏是一款涉及豆类的唱首歌大亨游戏。与 cookie Clicker 非常相似。 下面是完整的代码

import customtkinter as ctk
from customtkinter import CTkImage,CTkButton,CTkLabel
#from PIL import Image
import time as t

# stores money
money = 0
# variables for bean generation
beans = 0
click = 1
#toggles bean collection
toggle=False
#Application
class BeanApp(ctk.CTk):
    def __init__(self):
        super().__init__()
        # sets window and frames
        self.title("Bean Tycoon")
        self.geometry("500x500+700+200")

        #click modes
        multiplier_lbl=CTkLabel(self,text= "Multipliers")
        multiplier_lbl.place(x=250,y=1,anchor="n",)
        def xOne():#x1 multiplier toggle
            global click
            click=1
            one_click_mode_btn.configure(state="disabled")
            two_click_mode_btn.configure(state="enable")
            click_multiplyer_lbl.configure(text=f"  Beans/click: x{click}   ")
        def xTwo():#x2 multiplier toggle
            global click
            click=2
            one_click_mode_btn.configure(state="enable")
            two_click_mode_btn.configure(state="disabled")
            click_multiplyer_lbl.configure(text=f"  Beans/click: x{click}   ")

        one_click_mode_btn= CTkButton(self,text="x1",width=20,height=10,command=xOne,state="disabled")
        one_click_mode_btn.place(x=145,y=25,anchor="nw")
        two_click_mode_btn=CTkButton(self, text="x2",width=20,height=10,command=xTwo,state="disabled")
        two_click_mode_btn.place(x=173,y=25,anchor="nw")
        click_multiplyer_lbl=CTkLabel(self,text=f"  Beans/click: x{click}   ")
        click_multiplyer_lbl.place(x=3,y=45,anchor="nw",)
        # Bean generator
        #beanbtn = CTkImage(Image.open("Pictures\TheBean.png"),size=(200,200))
        bean_amt_lbl = CTkLabel(self,text= f"  Beans: {beans}  ",)
        bean_amt_lbl.place(x=3,y=5,anchor="nw")
        def clicked():
            global beans
            global click
            beans += click
            bean_amt_lbl.configure(text= f"  Beans: {beans}  ")

        sell_beans_btn = CTkButton(self,text= "",image=None, command= clicked,width=180,height=180) 
        sell_beans_btn.place(x=250,y=330, anchor="s")
        # Sell Beans
        money_amt_lbl = CTkLabel(self,text=f"  Money: ${money}  ", )
        money_amt_lbl.place(x=3,y=25,anchor='nw')
        def sell():
            global money
            global beans
            while beans >0:
                money = money+beans
                beans = 0
                bean_amt_lbl.configure(text=f"  Beans: {beans}  ")
                money_amt_lbl.configure(text=f"  Money: ${money  }")
                
        sell_bean_btn = CTkButton(self,text="Sell Beans",image=None, command=sell)
        sell_bean_btn.place(x=250,y=360,anchor='s')
        #2 times multiplier
        def d_b_u():#double_bean_upgrade
            global click,money
            if money>=100:
                click=2
                money=money-100
                money_amt_lbl.configure(text=f"  Money: ${money  }")
                double_bean_upgrade_btn.configure(state="disabled")
                two_click_mode_btn.configure(state="disabled")
                one_click_mode_btn.configure(state="enable")
                click_multiplyer_lbl.configure(text=f"  Beans/click: x{click}   ")
        #automatic bean collector
        def a_b_c_u():#automatic_bean_collector_upgrade
            global beans,money,toggle
            if money>=20:
                money=money-20
                money_amt_lbl.configure(text=f"  Money: ${money  }")
                auto_collect_bean_btn.configure(state="disabled")
                while beans >=0 :
                    beans=beans+1
                    bean_amt_lbl.configure(text=f"  Beans: {beans}  " )
                    t.sleep(3)
                    print(beans) 
                               
        #Shop
        shop_lbl= CTkLabel(self,text="Shop")
        shop_lbl.place(x=425,y=5,anchor="nw")
        double_bean_upgrade_btn = CTkButton(self,text="Bean Doubler\n$100",command=d_b_u,width=20,corner_radius=20)
        double_bean_upgrade_btn.place(x=390,y=30,anchor="nw")
        auto_collect_bean_btn = CTkButton(self,text="Auto Collect 1\n$200",command=a_b_c_u,width=20,corner_radius=20)
        auto_collect_bean_btn.place(x=390,y=70,anchor="nw")


if __name__ == "__main__":
    app = BeanApp()
    app.mainloop()

游戏涉及一个在后台运行的增量计数器,循环通过一个按钮切换。 但是,当循环激活时,它会冻结应用程序。这怎么能解决?

编辑:我标记了图像库和图像函数,因为它现在对代码没有丝毫影响。

编辑 2:如何正确实施线程?我不认为我这样做是对的...

  #automatic bean collector
    def b_c_():
        global beans
        while beans >=0 :
                beans=beans+1
                bean_amt_lbl.configure(text=f"  Beans: {beans}  " )
                t.sleep(3)
                print(beans) 
    bean_collector=Thread(target= b_c_)
    def a_b_c_u():#automatic_bean_collector_upgrade
        global beans,money,toggle
        if money>=20:
            money=money-20
            money_amt_lbl.configure(text=f"  Money: ${money  }")
            auto_collect_bean_btn.configure(state="disabled")
            bean_collector.run()

编辑 3:由于偏离原始问题而被删除

python while-loop freeze customtkinter
3个回答
0
投票

你需要将循环放到另一个线程中,这样用户界面就不会卡住。您可以使用

thread
库。

import thread

def foo(args):
  # loop logic

thread.start_new_thread(foo, (args))

0
投票

不阻塞 UI 的一般解决方案是在 UI 实际上 has 更新时传递函数,根本不使用循环,或者非常少地使用循环。

你也有全局状态,这是不推荐的。要解决这两个问题,请添加更多类。

class Bank:
  def __init__(self):
    self.money = 0

  def deposit(self, amount, handler=None)
    ''' accepts optional function with the amount deposited '''
    self.money += amount
    if handler:
      handler(amount)

  def withdraw(self, amount, handler=None)
    ''' accepts optional function with the amount withdrawn '''
    if self.money >= amount:
      self.money -= amount
      if handler:
        handler(amount)
    else:
      print('error: not enough money available')
class BeanCounter:
  def __init__(self, amount=0):
    self.amount = amount

  def increment(self, handler=None):
    ''' accepts optional function called after one bean added '''
    self.amount += 1
    if handler:
      handler()

然后更新应用程序构造函数并使用上面定义一些功能 功能

class BeanApp(ctk.CTk):
  def __init__(self, bank, bean_counter):
      super().__init__()
      # sets window and frames
      self.title("Bean Tycoon")

      self.bank = bank
      self.bean_counter = bean_counter

    def bean_label_updater(self):
      ''' updates the bean label with the current amount '''  
      bean_amt_lbl.configure(text= f"  Beans: {self.bean_counter.amount}  ")

    def on_click(self):
      self.bean_counter.increment(bean_label_updater)
    # TODO: button target=on_click

    def on_click_sell(self):
      self.bank.deposit(self.bean_counter.amount)  # times amount per bean ; todo: add bank label updater function
      self.bean_counter.amount = 0
      self.bean_label_updater()

然后

if __name__ == "__main__":
  bank = Bank()
  bean_counter = BeanCounter()

  app = BeanApp(bank, bean_counter)
  app.mainloop()

这不会完全解决“自动收集器”阻塞 UI 的问题,但它至少为您指明了一个将“状态/模型”与“视图”分离的一般起点

一个解决方案是像这样围绕 beancounter 类创建一个线程包装器,然后在关联的操作上启动它。

class BeanDoubler(Thread):
  def __init__(self, bean_counter, update_func=None):
    self.bean_counter = bean_counter
    self.update_func = update_func
  def run(self):
    while True:
      # use a for loop to increment over the amount (thereby doubling)
      for _ in range(self.bean_counter.amount):
        self.bean_counter.increment(self.update_func)

(代码未经测试,如有错别字请见谅)


0
投票

我现在似乎得到的只是一个定义的对象,没有给我给出的答案的属性。我现在无法将对象及其属性传递给

class BeanApp()
as shown here。我有点明白我哪里出错了,这在here中有解释。值得庆幸的是,用户界面现在运行良好

详细说明,

bank
bean_doubler
class BeanApp(self,bank,bean_doubler)
根据我的理解,当它们应该连接到
class Bank()
class BeanDoubler()
时是空变量,然后在类 BeanApp()` 初始化的窗口中使用它们中的函数。

© www.soinside.com 2019 - 2024. All rights reserved.