鱼C论坛

 找回密码
 立即注册
查看: 1659|回复: 15

[已解决]pygame 和 ttk 的问题

[复制链接]
发表于 2023-7-20 23:23:25 | 显示全部楼层 |阅读模式
10鱼币
本帖最后由 cjjJasonchen 于 2023-7-21 10:30 编辑
  1. <blockquote><div class="blockcode"><blockquote>""" 这个版本是专门用来体验的,如果要追求性能请调节SpurtFire的timer"""

  2. import pygame
  3. import sys
  4. import random
  5. import ttkbootstrap as tk
  6. import ttkbootstrap.constants as tkc
  7. from pygame.locals import *


  8. class Particle(pygame.sprite.Sprite):
  9.     def __init__(self,color=(255,255,255),position=[0,0],radius=0,speed=[0,0],time=-1):
  10.         super().__init__()
  11.         self.position = position
  12.         self.image = pygame.Surface([radius*2,radius*2]).convert_alpha()
  13.         self.rect = self.image.get_rect()
  14.         self.rect.centerx,self.rect.centery = position[0],position[1]
  15.         self.color = color
  16.         self.radius = radius
  17.         self.speed = speed
  18.         self.time = time

  19.         pygame.draw.rect(self.image,(0,0,0,0),[0,0,radius * 2,radius * 2])

  20.         

  21.     def update(self):
  22.         self.time -= 1
  23.         self.rect.centerx += self.speed[0]
  24.         self.rect.centery += self.speed[1]
  25.         pygame.draw.circle(self.image,
  26.                            color=self.color,
  27.                            center=(self.radius,self.radius),
  28.                            radius=self.radius)


  29. class SpurtFire(Particle):
  30.     def __init__(self, position,
  31.                  color=[255,255,255,255],
  32.                  time=[45,120],
  33.                  speed=[[-2,2],[-2,-2]],
  34.                  num = 4,
  35.                  radius=[1,4],
  36.                  burn_time=600,
  37.                  timer = 0):
  38.         self.particles = pygame.sprite.Group()
  39.         self.position = position
  40.         self.color = color
  41.         self.time = time
  42.         self.speed = speed
  43.         self.num = num
  44.         self.radius = radius
  45.         self.burn_time = burn_time
  46.         self.timer0 = timer
  47.         self.timer = timer

  48.     def update(self):
  49.             self.particles.update()
  50.             for i in self.particles:
  51.                 if i.time == 0:
  52.                     self.particles.remove(i)
  53.                 i.speed = [random.randint(self.speed[0][0],self.speed[0][1]),random.randint(self.speed[1][0],self.speed[1][1])]
  54.                 i.color[3] = self.color[3]/self.time[1]*i.time
  55.             if self.timer == 0:
  56.                 self.timer = self.timer0
  57.                 self.create_particle()
  58.             else:
  59.                 self.timer -= 1
  60.             self.burn_time -= 1
  61.         

  62.     def draw(self,screen):
  63.         self.particles.draw(screen)
  64.         
  65.     def create_particle(self):
  66.         for i in range(self.num):
  67.             self.particles.add(Particle(color=self.color.copy(),
  68.                                     position=[self.position[0],self.position[1]],
  69.                                     radius=random.randint(self.radius[0],self.radius[1]),
  70.                                     speed=[random.randint(self.speed[0][0],self.speed[0][1]),random.randint(self.speed[1][0],self.speed[1][1])],
  71.                                     time = random.randint(self.time[0],self.time[1])
  72.                                         )
  73.                                )
  74.         

  75. def set_up():
  76.     color = {"outside":[85,85,85,20],
  77.              "inside":[170,170,170,10],
  78.              "core":[255,255,255,10],
  79.              "bg":[0,0,0]}
  80.     num_list = [50,25,12]
  81.     size = [1,10]
  82.    

  83.    
  84.     root = tk.Window(themename="superhero")
  85.     root.geometry("700x700+300+10")

  86.     tk.Label(root, text="请输入外焰、內焰和焰芯的颜色",font=("楷体", 30)).pack(padx=5,pady=10)

  87.     key_l = tk.Label(root, text="点它!——>    (红)(绿)(蓝)(透明度)    <——点它!",font=("楷体", 15))
  88.     key_l.pack(padx = 5,pady = 5)

  89.     frame1 = tk.Frame(root,width = 400,height = 300)
  90.     frame1.pack(padx=5,pady=5)


  91.     tk.Label(frame1, text="",font=("楷体", 40)).grid(row=0,column=0)
  92.     # 外焰
  93.     tk.Label(frame1, text="外焰颜色(RGBA):(",font=("楷体", 20)).grid(row=1,column=0)
  94.    
  95.     outer_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  96.     outer_e1.grid(row=1,column=1)

  97.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=2)

  98.     outer_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  99.     outer_e2.grid(row=1,column=3)

  100.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=4)

  101.    
  102.     outer_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  103.     outer_e3.grid(row=1,column=5)

  104.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=6)

  105.     outer_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  106.     outer_e4.grid(row=1,column=7)

  107.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=1,column=8)
  108.    
  109.    
  110.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=2,column=0)
  111.     # 内焰
  112.     tk.Label(frame1, text="内焰颜色(RGBA):(",font=("楷体", 20)).grid(row=3,column=0)
  113.    
  114.     inside_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  115.     inside_e1.grid(row=3,column=1)

  116.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=2)

  117.     inside_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  118.     inside_e2.grid(row=3,column=3)

  119.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=4)

  120.    
  121.     inside_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  122.     inside_e3.grid(row=3,column=5)

  123.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=6)

  124.     inside_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  125.     inside_e4.grid(row=3,column=7)

  126.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=3,column=8)


  127.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=4,column=0)
  128.     # 焰芯
  129.     tk.Label(frame1, text="焰芯颜色(RGBA):(",font=("楷体", 20)).grid(row=5,column=0)
  130.    
  131.     core_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  132.     core_e1.grid(row=5,column=1)

  133.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=2)

  134.     core_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  135.     core_e2.grid(row=5,column=3)

  136.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=4)

  137.    
  138.     core_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  139.     core_e3.grid(row=5,column=5)

  140.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=6)

  141.     core_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  142.     core_e4.grid(row=5,column=7)

  143.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=5,column=8)


  144.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=6,column=0)
  145.     # 粒子数量
  146.     num = tk.IntVar()
  147.     num.set(50)
  148.    
  149.     particel_num = tk.StringVar()
  150.     particel_num.set(f"粒子数量:外焰{int(num.get())}、內焰{int(num.get()/2)}、焰芯{int(num.get()/4)}")
  151.    
  152.     tk.Label(frame1, textvariable=particel_num, font=("楷体", 20)).grid(row=7, column=0,columnspan=7)
  153.     tk.Label(frame1, text="少",style="primary", font=("楷体", 15)).grid(row=8,column=0)
  154.     tk.Label(frame1, text="多",style="primary", font=("楷体", 15)).grid(row=8,column=7)

  155.     def change_particle_num(event):
  156.         particel_num.set(f"粒子数量:外焰{int(num.get())}、內焰{int(num.get()/2)}、焰芯{int(num.get()/4)}")
  157.    
  158.     num_s = tk.Scale(frame1, from_=10, to=80, length=300, variable=num,command=change_particle_num)
  159.     num_s.grid(row=8,column=0,columnspan=7,padx=5,pady=5,sticky="e")


  160.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=9,column=0)
  161.     # 粒子大小

  162.     tk.Label(frame1, text="粒子数量:", font=("楷体", 20)).grid(row=10, column=0)
  163.     tk.Label(frame1, text="最小:",style="primary", font=("楷体", 15)).grid(row=10,column=1)
  164.     tk.Label(frame1, text="最大:",style="primary", font=("楷体", 15)).grid(row=10,column=4)

  165.     size_e1 = tk.Entry(frame1, width=3,font=("楷体", 15))
  166.     size_e1.grid(row=10,column=2,padx=5,pady=5,sticky="e")

  167.     size_e2 = tk.Entry(frame1, width=3,font=("楷体", 15))
  168.     size_e2.grid(row=10,column=5,padx=5,pady=5)
  169.    

  170.     # 背景
  171.     tk.Label(frame1, text="背景\n更改\n   |\n  ﹀",style="primary").grid(row=0,column=9)
  172.    
  173.     tk.Button(frame1, text="白",style="light").grid(row=10,column=9,padx=10)

  174.     tk.Button(frame1, text="黑",style="secondary").grid(row=1,column=9,padx=10)
  175.    
  176.     bg_s = tk.Scale(frame1, from_=0, to=255, orient=tk.VERTICAL, length=300,style="light")
  177.     bg_s.grid(row=1,rowspan=10,column=9,padx=30,pady=10)


  178.    

  179.     # 确定

  180.     def return_color():
  181.         try:
  182.             color["outside"] = [int(outer_e1.get()),int(outer_e2.get()),int(outer_e3.get()),int(outer_e4.get())]
  183.             color["inside"] = [int(inside_e1.get()),int(inside_e2.get()),int(inside_e3.get()),int(inside_e4.get())]
  184.             color["core"] = [int(core_e1.get()),int(core_e2.get()),int(core_e3.get()),int(core_e4.get())]
  185.             color["bg"] = [int(bg_s.get()),int(bg_s.get()),int(bg_s.get())]

  186.             num_list[0]=int(num_s.get())
  187.             num_list[1]=int(num_s.get()/2)
  188.             num_list[2]=int(num_s.get()/4)

  189.             size[0]=int(size_e1.get())
  190.             size[1]=int(size_e2.get())
  191.         except:
  192.             pass
  193.         root.destroy()
  194.         
  195.    
  196.     tk.Style().configure('my.TButton', font=('楷体', 35))
  197.     b = tk.Button(root, text='确定', style='my.TButton',command=return_color)
  198.     b.pack(padx=10,pady = 20,fill="both")

  199.     root.mainloop()

  200.     return color,num_list,size



  201. if __name__ == "__main__":
  202.     size = width, height = 800,600

  203.     screen = pygame.display.set_mode(size)

  204.     pygame.display.set_caption("title")

  205.     clock = pygame.time.Clock()

  206.     delay = 60 # 延时计时器
  207.     time = 0

  208.     # 是否全屏
  209.     fullscreen = False
  210.     screen_change = False

  211.     running = True


  212.     # 粒子效果列表
  213.     particles = []

  214.     # 颜色,数量,大小
  215.     color = {"outside":[0,0,255,30],
  216.              "inside":[0,255,255,10],
  217.              "core":[200,200,255,10],
  218.              "bg":[0,0,0]}
  219.     num = [80,40,20]

  220.     particle_size = [1,10]


  221.     while running:
  222.         clock.tick(60)

  223.         # 得到鼠标位置
  224.         pos = pygame.mouse.get_pos()

  225.         # 检测是否全屏
  226.         if fullscreen and screen_change:
  227.             screen = pygame.display.set_mode(size,FULLSCREEN,HWSURFACE)
  228.             screen_change = False
  229.         elif screen_change:
  230.             screen = pygame.display.set_mode(size)
  231.             screen_change = False

  232.         for event in pygame.event.get():
  233.             if event.type == QUIT:
  234.                 pygame.quit()
  235.                 sys.exit()
  236.                
  237.             if event.type == MOUSEBUTTONDOWN:
  238.                 if event.button == 1:
  239.                     particles.append(SpurtFire(pos,
  240.                                                color=color["outside"],
  241.                                                time=[1,80],
  242.                                                speed=[[-3,+3],[-2,-1]],
  243.                                                num = num[0],
  244.                                                radius = particle_size,
  245.                                                burn_time = 1200,
  246.                                                timer=0))
  247.                     particles.append(SpurtFire(pos,
  248.                                                color=color["inside"],
  249.                                                time=[1,70],
  250.                                                speed=[[-2,+2],[-2,-1]],
  251.                                                num = num[1],
  252.                                                radius = particle_size,
  253.                                                burn_time = 1200,
  254.                                                timer=0))
  255.                     particles.append(SpurtFire(pos,
  256.                                                color=color["core"],
  257.                                                time=[1,50],
  258.                                                speed=[[-1,+1],[-2,-1]],
  259.                                                num = num[2],
  260.                                                radius = particle_size,
  261.                                                burn_time = 1200,
  262.                                                timer=0))

  263.                     
  264.                     


  265.             if event.type == KEYDOWN:
  266.                 if event.key == K_ESCAPE:
  267.                     pygame.quit()
  268.                     sys.exit()
  269.                     
  270.                 #F11切换全屏
  271.                 elif event.key == K_F11:
  272.                     fullscreen = not fullscreen
  273.                     screen_change = True

  274.                 elif event.key == K_1:
  275.                     bg = (255,255,255)

  276.                 elif event.key == K_2:
  277.                     bg = (0,0,0)

  278.                 elif event.key == K_o:
  279.                     color,num,particle_size=set_up()
  280.                     pygame.event.clear()
  281.                     

  282.                 elif event.key == K_c:
  283.                     particles.clear()


  284.         #画背景
  285.         screen.fill(color["bg"])

  286.         #画 xxxx
  287.         for particle in particles:
  288.             particle.draw(screen)

  289.         #刷新
  290.         for particle in particles:
  291.             particle.update()
  292.             if not particle.burn_time:
  293.                 particles.remove(particle)

  294.         # 刷新界面
  295.         pygame.display.update()



复制代码



点击o会生成一个ttk界面,但是从第二次开始他的风格变得不一样了,看起来像tkinter的默认主题

再补充一下,主题看起来是对的,但是组件的风格看起来不对,上图:

正常的

正常的

第二次开始

第二次开始


我寻思这可能就是ttk自己有问题啊。。。

看看这个:

  1. import ttkbootstrap as ttk


  2. class A():
  3.     def __init__(self):
  4.         
  5.         #input("回车创建窗口")
  6.         self.root=ttk.Window()
  7.         self.root.geometry("700x700+300+10")
  8.         ttk.Button(self.root,text="关闭",command=self.callback).pack()
  9.         self.root.mainloop()
  10.         print(1)

  11.     def callback(self):
  12.         self.root.destroy()



  13.         

  14. if __name__ == "__main__":
  15.     A()
  16.     A()
复制代码


明明是一样的,我甚至没有设置主题。。。为什么两次出来的不一样



最佳答案
2023-7-20 23:23:26


  1. import pygame
  2. import sys
  3. import random
  4. import tkinter as tkinter
  5. import ttkbootstrap as tk
  6. import ttkbootstrap.constants as tkc
  7. from pygame.locals import *
  8. from threading import Thread, current_thread


  9. class Particle(pygame.sprite.Sprite):
  10.     def __init__(self,color=(255,255,255),position=[0,0],radius=0,speed=[0,0],time=-1):
  11.         super().__init__()
  12.         self.position = position
  13.         self.image = pygame.Surface([radius*2,radius*2]).convert_alpha()
  14.         self.rect = self.image.get_rect()
  15.         self.rect.centerx,self.rect.centery = position[0],position[1]
  16.         self.color = color
  17.         self.radius = radius
  18.         self.speed = speed
  19.         self.time = time

  20.         pygame.draw.rect(self.image,(0,0,0,0),[0,0,radius * 2,radius * 2])

  21.         

  22.     def update(self):
  23.         self.time -= 1
  24.         self.rect.centerx += self.speed[0]
  25.         self.rect.centery += self.speed[1]
  26.         pygame.draw.circle(self.image,
  27.                            color=self.color,
  28.                            center=(self.radius,self.radius),
  29.                            radius=self.radius)


  30. class SpurtFire(Particle):
  31.     def __init__(self, position,
  32.                  color=[255,255,255,255],
  33.                  time=[45,120],
  34.                  speed=[[-2,2],[-2,-2]],
  35.                  num = 4,
  36.                  radius=[1,4],
  37.                  burn_time=600,
  38.                  timer = 0):
  39.         self.particles = pygame.sprite.Group()
  40.         self.position = position
  41.         self.color = color
  42.         self.time = time
  43.         self.speed = speed
  44.         self.num = num
  45.         self.radius = radius
  46.         self.burn_time = burn_time
  47.         self.timer0 = timer
  48.         self.timer = timer

  49.     def update(self):
  50.             self.particles.update()
  51.             for i in self.particles:
  52.                 if i.time == 0:
  53.                     self.particles.remove(i)
  54.                 i.speed = [random.randint(self.speed[0][0],self.speed[0][1]),random.randint(self.speed[1][0],self.speed[1][1])]
  55.                 i.color[3] = self.color[3]/self.time[1]*i.time
  56.             if self.timer == 0:
  57.                 self.timer = self.timer0
  58.                 self.create_particle()
  59.             else:
  60.                 self.timer -= 1
  61.             self.burn_time -= 1
  62.         

  63.     def draw(self,screen):
  64.         self.particles.draw(screen)
  65.         
  66.     def create_particle(self):
  67.         for i in range(self.num):
  68.             self.particles.add(Particle(color=self.color.copy(),
  69.                                     position=[self.position[0],self.position[1]],
  70.                                     radius=random.randint(self.radius[0],self.radius[1]),
  71.                                     speed=[random.randint(self.speed[0][0],self.speed[0][1]),random.randint(self.speed[1][0],self.speed[1][1])],
  72.                                     time = random.randint(self.time[0],self.time[1])
  73.                                         )
  74.                                )

  75. # 颜色,数量,大小
  76. color = {"outside":[0,0,255,30],
  77.             "inside":[0,255,255,10],
  78.             "core":[200,200,255,10],
  79.             "bg":[0,0,0]}
  80. num = [80,40,20]

  81. particle_size = [1,10]

  82. def set_up():
  83.     global root
  84.     color = {"outside":[85,85,85,20],
  85.              "inside":[170,170,170,10],
  86.              "core":[255,255,255,10],
  87.              "bg":[0,0,0]}
  88.     num_list = [50,25,12]
  89.     size = [1,10]
  90.    

  91.    
  92.     root = tk.Window(themename="superhero")
  93.             
  94.     root.geometry("700x700+300+10")

  95.     root.update()


  96.    

  97.     tk.Label(root, text="请输入外焰、內焰和焰芯的颜色",font=("楷体", 30)).pack(padx=5,pady=10)

  98.     key_l = tk.Label(root, text="点它!——>    (红)(绿)(蓝)(透明度)    <——点它!",font=("楷体", 15))
  99.     key_l.pack(padx = 5,pady = 5)

  100.     frame1 = tk.Frame(root,width = 400,height = 300)
  101.     frame1.pack(padx=5,pady=5)


  102.     tk.Label(frame1, text="",font=("楷体", 40)).grid(row=0,column=0)
  103.     # 外焰
  104.     tk.Label(frame1, text="外焰颜色(RGBA):(",font=("楷体", 20)).grid(row=1,column=0)
  105.    
  106.     outer_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  107.     outer_e1.grid(row=1,column=1)

  108.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=2)

  109.     outer_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  110.     outer_e2.grid(row=1,column=3)

  111.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=4)

  112.    
  113.     outer_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  114.     outer_e3.grid(row=1,column=5)

  115.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=6)

  116.     outer_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  117.     outer_e4.grid(row=1,column=7)

  118.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=1,column=8)
  119.    
  120.    
  121.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=2,column=0)
  122.     # 内焰
  123.     tk.Label(frame1, text="内焰颜色(RGBA):(",font=("楷体", 20)).grid(row=3,column=0)
  124.    
  125.     inside_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  126.     inside_e1.grid(row=3,column=1)

  127.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=2)

  128.     inside_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  129.     inside_e2.grid(row=3,column=3)

  130.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=4)

  131.    
  132.     inside_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  133.     inside_e3.grid(row=3,column=5)

  134.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=6)

  135.     inside_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  136.     inside_e4.grid(row=3,column=7)

  137.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=3,column=8)


  138.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=4,column=0)
  139.     # 焰芯
  140.     tk.Label(frame1, text="焰芯颜色(RGBA):(",font=("楷体", 20)).grid(row=5,column=0)
  141.    
  142.     core_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  143.     core_e1.grid(row=5,column=1)

  144.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=2)

  145.     core_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  146.     core_e2.grid(row=5,column=3)

  147.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=4)

  148.    
  149.     core_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  150.     core_e3.grid(row=5,column=5)

  151.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=6)

  152.     core_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  153.     core_e4.grid(row=5,column=7)

  154.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=5,column=8)


  155.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=6,column=0)
  156.     # 粒子数量
  157.     num = tk.IntVar()
  158.     num.set(50)
  159.    
  160.     particel_num = tk.StringVar()
  161.     particel_num.set(f"粒子数量:外焰{int(num.get())}、內焰{int(num.get()/2)}、焰芯{int(num.get()/4)}")
  162.    
  163.     tk.Label(frame1, textvariable=particel_num, font=("楷体", 20)).grid(row=7, column=0,columnspan=7)
  164.     tk.Label(frame1, text="少",style="primary", font=("楷体", 15)).grid(row=8,column=0)
  165.     tk.Label(frame1, text="多",style="primary", font=("楷体", 15)).grid(row=8,column=7)

  166.     def change_particle_num(event):
  167.         particel_num.set(f"粒子数量:外焰{int(num.get())}、內焰{int(num.get()/2)}、焰芯{int(num.get()/4)}")
  168.    
  169.     num_s = tk.Scale(frame1, from_=10, to=80, length=300, variable=num,command=change_particle_num)
  170.     num_s.grid(row=8,column=0,columnspan=7,padx=5,pady=5,sticky="e")


  171.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=9,column=0)
  172.     # 粒子大小

  173.     tk.Label(frame1, text="粒子数量:", font=("楷体", 20)).grid(row=10, column=0)
  174.     tk.Label(frame1, text="最小:",style="primary", font=("楷体", 15)).grid(row=10,column=1)
  175.     tk.Label(frame1, text="最大:",style="primary", font=("楷体", 15)).grid(row=10,column=4)

  176.     size_e1 = tk.Entry(frame1, width=3,font=("楷体", 15))
  177.     size_e1.grid(row=10,column=2,padx=5,pady=5,sticky="e")

  178.     size_e2 = tk.Entry(frame1, width=3,font=("楷体", 15))
  179.     size_e2.grid(row=10,column=5,padx=5,pady=5)
  180.    

  181.     # 背景
  182.     tk.Label(frame1, text="背景\n更改\n   |\n  ﹀",style="primary").grid(row=0,column=9)
  183.    
  184.     tk.Button(frame1, text="白",style="light").grid(row=10,column=9,padx=10)

  185.     tk.Button(frame1, text="黑",style="secondary").grid(row=1,column=9,padx=10)
  186.    
  187.     bg_s = tk.Scale(frame1, from_=0, to=255, orient=tk.VERTICAL, length=300,style="light")
  188.     bg_s.grid(row=1,rowspan=10,column=9,padx=30,pady=10)


  189.    

  190.     # 确定


  191.     def return_color():
  192.         try:
  193.             global color,num,particle_size
  194.             color["outside"] = [int(outer_e1.get()),int(outer_e2.get()),int(outer_e3.get()),int(outer_e4.get())]
  195.             color["inside"] = [int(inside_e1.get()),int(inside_e2.get()),int(inside_e3.get()),int(inside_e4.get())]
  196.             color["core"] = [int(core_e1.get()),int(core_e2.get()),int(core_e3.get()),int(core_e4.get())]
  197.             color["bg"] = [int(bg_s.get()),int(bg_s.get()),int(bg_s.get())]

  198.             num_list[0]=int(num_s.get())
  199.             num_list[1]=int(num_s.get()/2)
  200.             num_list[2]=int(num_s.get()/4)

  201.             size[0]=int(size_e1.get())
  202.             size[1]=int(size_e2.get())
  203.             # outer_e1.set()
  204.         except:
  205.             pass

  206.         root.withdraw()
  207.         # root.update()
  208.         # root.protocol("WM_DELETE_WINDOW", on_closing)
  209.         # root.destroy()
  210.         # if not root:
  211.         #     return

  212.         
  213.    
  214.     tk.Style().configure('my.TButton', font=('楷体', 35))
  215.     b = tk.Button(root, text='确定', style='my.TButton',command=return_color)
  216.     b.pack(padx=10,pady = 20,fill="both")

  217.     root.mainloop()
  218.     # root_L.mainloop()

  219.    
  220. if_start = 0


  221. if __name__ == "__main__":
  222.     size = width, height = 800,600

  223.     screen = pygame.display.set_mode(size)

  224.     pygame.display.set_caption("title")

  225.     clock = pygame.time.Clock()

  226.     delay = 60 # 延时计时器
  227.     time = 0

  228.     # 是否全屏
  229.     fullscreen = False
  230.     screen_change = False

  231.     running = True


  232.     # 粒子效果列表
  233.     particles = []




  234.     while running:
  235.         clock.tick(60)

  236.         # 得到鼠标位置
  237.         pos = pygame.mouse.get_pos()

  238.         # 检测是否全屏
  239.         if fullscreen and screen_change:
  240.             screen = pygame.display.set_mode(size,FULLSCREEN,HWSURFACE)
  241.             screen_change = False
  242.         elif screen_change:
  243.             screen = pygame.display.set_mode(size)
  244.             screen_change = False

  245.         for event in pygame.event.get():
  246.             if event.type == QUIT:
  247.                 pygame.quit()
  248.                 # root.destroy()
  249.                 exit()
  250.                
  251.             if event.type == MOUSEBUTTONDOWN:
  252.                 if event.button == 1:
  253.                     particles.append(SpurtFire(pos,
  254.                                                color=color["outside"],
  255.                                                time=[1,80],
  256.                                                speed=[[-3,+3],[-2,-1]],
  257.                                                num = num[0],
  258.                                                radius = particle_size,
  259.                                                burn_time = 1200,
  260.                                                timer=0))
  261.                     particles.append(SpurtFire(pos,
  262.                                                color=color["inside"],
  263.                                                time=[1,70],
  264.                                                speed=[[-2,+2],[-2,-1]],
  265.                                                num = num[1],
  266.                                                radius = particle_size,
  267.                                                burn_time = 1200,
  268.                                                timer=0))
  269.                     particles.append(SpurtFire(pos,
  270.                                                color=color["core"],
  271.                                                time=[1,50],
  272.                                                speed=[[-1,+1],[-2,-1]],
  273.                                                num = num[2],
  274.                                                radius = particle_size,
  275.                                                burn_time = 1200,
  276.                                                timer=0))

  277.                     
  278.                     


  279.             if event.type == KEYDOWN:
  280.                 if event.key == K_ESCAPE:
  281.                     pygame.quit()
  282.                     root.destroy()
  283.                     sys.exit()
  284.                     
  285.                 #F11切换全屏
  286.                 elif event.key == K_F11:
  287.                     fullscreen = not fullscreen
  288.                     screen_change = True

  289.                 elif event.key == K_1:
  290.                     bg = (255,255,255)

  291.                 elif event.key == K_2:
  292.                     bg = (0,0,0)

  293.                 elif event.key == K_o:
  294.                     # color,num,particle_size=thread01=Thread(target=set_up, name="线程1")
  295.                     if (if_start == 0):
  296.                         thread01=Thread(target=set_up, name="线程1")
  297.                         thread01.start()
  298.                         if_start = 1
  299.                     else:
  300.                         root.update()
  301.                         root.deiconify()
  302.                     pygame.event.clear()
  303.                     

  304.                 elif event.key == K_c:
  305.                     particles.clear()


  306.         #画背景
  307.         screen.fill(color["bg"])

  308.         #画 xxxx
  309.         for particle in particles:
  310.             particle.draw(screen)

  311.         #刷新
  312.         for particle in particles:
  313.             particle.update()
  314.             if not particle.burn_time:
  315.                 particles.remove(particle)

  316.         # 刷新界面
  317.         pygame.display.update()

  318.         

复制代码


使用多线程来解决这个办法

然后再打开还会保留上一次的数据,你自己写把内容清空就行了

如果对你有帮助的话,给个最佳答案呗!!!

最佳答案

查看完整内容

使用多线程来解决这个办法 然后再打开还会保留上一次的数据,你自己写把内容清空就行了 如果对你有帮助的话,给个最佳答案呗!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-20 23:23:26 | 显示全部楼层    本楼为最佳答案   


  1. import pygame
  2. import sys
  3. import random
  4. import tkinter as tkinter
  5. import ttkbootstrap as tk
  6. import ttkbootstrap.constants as tkc
  7. from pygame.locals import *
  8. from threading import Thread, current_thread


  9. class Particle(pygame.sprite.Sprite):
  10.     def __init__(self,color=(255,255,255),position=[0,0],radius=0,speed=[0,0],time=-1):
  11.         super().__init__()
  12.         self.position = position
  13.         self.image = pygame.Surface([radius*2,radius*2]).convert_alpha()
  14.         self.rect = self.image.get_rect()
  15.         self.rect.centerx,self.rect.centery = position[0],position[1]
  16.         self.color = color
  17.         self.radius = radius
  18.         self.speed = speed
  19.         self.time = time

  20.         pygame.draw.rect(self.image,(0,0,0,0),[0,0,radius * 2,radius * 2])

  21.         

  22.     def update(self):
  23.         self.time -= 1
  24.         self.rect.centerx += self.speed[0]
  25.         self.rect.centery += self.speed[1]
  26.         pygame.draw.circle(self.image,
  27.                            color=self.color,
  28.                            center=(self.radius,self.radius),
  29.                            radius=self.radius)


  30. class SpurtFire(Particle):
  31.     def __init__(self, position,
  32.                  color=[255,255,255,255],
  33.                  time=[45,120],
  34.                  speed=[[-2,2],[-2,-2]],
  35.                  num = 4,
  36.                  radius=[1,4],
  37.                  burn_time=600,
  38.                  timer = 0):
  39.         self.particles = pygame.sprite.Group()
  40.         self.position = position
  41.         self.color = color
  42.         self.time = time
  43.         self.speed = speed
  44.         self.num = num
  45.         self.radius = radius
  46.         self.burn_time = burn_time
  47.         self.timer0 = timer
  48.         self.timer = timer

  49.     def update(self):
  50.             self.particles.update()
  51.             for i in self.particles:
  52.                 if i.time == 0:
  53.                     self.particles.remove(i)
  54.                 i.speed = [random.randint(self.speed[0][0],self.speed[0][1]),random.randint(self.speed[1][0],self.speed[1][1])]
  55.                 i.color[3] = self.color[3]/self.time[1]*i.time
  56.             if self.timer == 0:
  57.                 self.timer = self.timer0
  58.                 self.create_particle()
  59.             else:
  60.                 self.timer -= 1
  61.             self.burn_time -= 1
  62.         

  63.     def draw(self,screen):
  64.         self.particles.draw(screen)
  65.         
  66.     def create_particle(self):
  67.         for i in range(self.num):
  68.             self.particles.add(Particle(color=self.color.copy(),
  69.                                     position=[self.position[0],self.position[1]],
  70.                                     radius=random.randint(self.radius[0],self.radius[1]),
  71.                                     speed=[random.randint(self.speed[0][0],self.speed[0][1]),random.randint(self.speed[1][0],self.speed[1][1])],
  72.                                     time = random.randint(self.time[0],self.time[1])
  73.                                         )
  74.                                )

  75. # 颜色,数量,大小
  76. color = {"outside":[0,0,255,30],
  77.             "inside":[0,255,255,10],
  78.             "core":[200,200,255,10],
  79.             "bg":[0,0,0]}
  80. num = [80,40,20]

  81. particle_size = [1,10]

  82. def set_up():
  83.     global root
  84.     color = {"outside":[85,85,85,20],
  85.              "inside":[170,170,170,10],
  86.              "core":[255,255,255,10],
  87.              "bg":[0,0,0]}
  88.     num_list = [50,25,12]
  89.     size = [1,10]
  90.    

  91.    
  92.     root = tk.Window(themename="superhero")
  93.             
  94.     root.geometry("700x700+300+10")

  95.     root.update()


  96.    

  97.     tk.Label(root, text="请输入外焰、內焰和焰芯的颜色",font=("楷体", 30)).pack(padx=5,pady=10)

  98.     key_l = tk.Label(root, text="点它!——>    (红)(绿)(蓝)(透明度)    <——点它!",font=("楷体", 15))
  99.     key_l.pack(padx = 5,pady = 5)

  100.     frame1 = tk.Frame(root,width = 400,height = 300)
  101.     frame1.pack(padx=5,pady=5)


  102.     tk.Label(frame1, text="",font=("楷体", 40)).grid(row=0,column=0)
  103.     # 外焰
  104.     tk.Label(frame1, text="外焰颜色(RGBA):(",font=("楷体", 20)).grid(row=1,column=0)
  105.    
  106.     outer_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  107.     outer_e1.grid(row=1,column=1)

  108.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=2)

  109.     outer_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  110.     outer_e2.grid(row=1,column=3)

  111.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=4)

  112.    
  113.     outer_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  114.     outer_e3.grid(row=1,column=5)

  115.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=1,column=6)

  116.     outer_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  117.     outer_e4.grid(row=1,column=7)

  118.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=1,column=8)
  119.    
  120.    
  121.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=2,column=0)
  122.     # 内焰
  123.     tk.Label(frame1, text="内焰颜色(RGBA):(",font=("楷体", 20)).grid(row=3,column=0)
  124.    
  125.     inside_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  126.     inside_e1.grid(row=3,column=1)

  127.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=2)

  128.     inside_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  129.     inside_e2.grid(row=3,column=3)

  130.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=4)

  131.    
  132.     inside_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  133.     inside_e3.grid(row=3,column=5)

  134.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=3,column=6)

  135.     inside_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  136.     inside_e4.grid(row=3,column=7)

  137.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=3,column=8)


  138.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=4,column=0)
  139.     # 焰芯
  140.     tk.Label(frame1, text="焰芯颜色(RGBA):(",font=("楷体", 20)).grid(row=5,column=0)
  141.    
  142.     core_e1 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  143.     core_e1.grid(row=5,column=1)

  144.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=2)

  145.     core_e2 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  146.     core_e2.grid(row=5,column=3)

  147.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=4)

  148.    
  149.     core_e3 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  150.     core_e3.grid(row=5,column=5)

  151.     tk.Label(frame1, text=")(",font=("楷体", 20)).grid(row=5,column=6)

  152.     core_e4 = tk.Entry(frame1,width=3,font=("楷体", 20),bootstyle="PRIMARY")
  153.     core_e4.grid(row=5,column=7)

  154.     tk.Label(frame1, text=")",font=("楷体", 20)).grid(row=5,column=8)


  155.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=6,column=0)
  156.     # 粒子数量
  157.     num = tk.IntVar()
  158.     num.set(50)
  159.    
  160.     particel_num = tk.StringVar()
  161.     particel_num.set(f"粒子数量:外焰{int(num.get())}、內焰{int(num.get()/2)}、焰芯{int(num.get()/4)}")
  162.    
  163.     tk.Label(frame1, textvariable=particel_num, font=("楷体", 20)).grid(row=7, column=0,columnspan=7)
  164.     tk.Label(frame1, text="少",style="primary", font=("楷体", 15)).grid(row=8,column=0)
  165.     tk.Label(frame1, text="多",style="primary", font=("楷体", 15)).grid(row=8,column=7)

  166.     def change_particle_num(event):
  167.         particel_num.set(f"粒子数量:外焰{int(num.get())}、內焰{int(num.get()/2)}、焰芯{int(num.get()/4)}")
  168.    
  169.     num_s = tk.Scale(frame1, from_=10, to=80, length=300, variable=num,command=change_particle_num)
  170.     num_s.grid(row=8,column=0,columnspan=7,padx=5,pady=5,sticky="e")


  171.     tk.Label(frame1, text="",font=("楷体", 30)).grid(row=9,column=0)
  172.     # 粒子大小

  173.     tk.Label(frame1, text="粒子数量:", font=("楷体", 20)).grid(row=10, column=0)
  174.     tk.Label(frame1, text="最小:",style="primary", font=("楷体", 15)).grid(row=10,column=1)
  175.     tk.Label(frame1, text="最大:",style="primary", font=("楷体", 15)).grid(row=10,column=4)

  176.     size_e1 = tk.Entry(frame1, width=3,font=("楷体", 15))
  177.     size_e1.grid(row=10,column=2,padx=5,pady=5,sticky="e")

  178.     size_e2 = tk.Entry(frame1, width=3,font=("楷体", 15))
  179.     size_e2.grid(row=10,column=5,padx=5,pady=5)
  180.    

  181.     # 背景
  182.     tk.Label(frame1, text="背景\n更改\n   |\n  ﹀",style="primary").grid(row=0,column=9)
  183.    
  184.     tk.Button(frame1, text="白",style="light").grid(row=10,column=9,padx=10)

  185.     tk.Button(frame1, text="黑",style="secondary").grid(row=1,column=9,padx=10)
  186.    
  187.     bg_s = tk.Scale(frame1, from_=0, to=255, orient=tk.VERTICAL, length=300,style="light")
  188.     bg_s.grid(row=1,rowspan=10,column=9,padx=30,pady=10)


  189.    

  190.     # 确定


  191.     def return_color():
  192.         try:
  193.             global color,num,particle_size
  194.             color["outside"] = [int(outer_e1.get()),int(outer_e2.get()),int(outer_e3.get()),int(outer_e4.get())]
  195.             color["inside"] = [int(inside_e1.get()),int(inside_e2.get()),int(inside_e3.get()),int(inside_e4.get())]
  196.             color["core"] = [int(core_e1.get()),int(core_e2.get()),int(core_e3.get()),int(core_e4.get())]
  197.             color["bg"] = [int(bg_s.get()),int(bg_s.get()),int(bg_s.get())]

  198.             num_list[0]=int(num_s.get())
  199.             num_list[1]=int(num_s.get()/2)
  200.             num_list[2]=int(num_s.get()/4)

  201.             size[0]=int(size_e1.get())
  202.             size[1]=int(size_e2.get())
  203.             # outer_e1.set()
  204.         except:
  205.             pass

  206.         root.withdraw()
  207.         # root.update()
  208.         # root.protocol("WM_DELETE_WINDOW", on_closing)
  209.         # root.destroy()
  210.         # if not root:
  211.         #     return

  212.         
  213.    
  214.     tk.Style().configure('my.TButton', font=('楷体', 35))
  215.     b = tk.Button(root, text='确定', style='my.TButton',command=return_color)
  216.     b.pack(padx=10,pady = 20,fill="both")

  217.     root.mainloop()
  218.     # root_L.mainloop()

  219.    
  220. if_start = 0


  221. if __name__ == "__main__":
  222.     size = width, height = 800,600

  223.     screen = pygame.display.set_mode(size)

  224.     pygame.display.set_caption("title")

  225.     clock = pygame.time.Clock()

  226.     delay = 60 # 延时计时器
  227.     time = 0

  228.     # 是否全屏
  229.     fullscreen = False
  230.     screen_change = False

  231.     running = True


  232.     # 粒子效果列表
  233.     particles = []




  234.     while running:
  235.         clock.tick(60)

  236.         # 得到鼠标位置
  237.         pos = pygame.mouse.get_pos()

  238.         # 检测是否全屏
  239.         if fullscreen and screen_change:
  240.             screen = pygame.display.set_mode(size,FULLSCREEN,HWSURFACE)
  241.             screen_change = False
  242.         elif screen_change:
  243.             screen = pygame.display.set_mode(size)
  244.             screen_change = False

  245.         for event in pygame.event.get():
  246.             if event.type == QUIT:
  247.                 pygame.quit()
  248.                 # root.destroy()
  249.                 exit()
  250.                
  251.             if event.type == MOUSEBUTTONDOWN:
  252.                 if event.button == 1:
  253.                     particles.append(SpurtFire(pos,
  254.                                                color=color["outside"],
  255.                                                time=[1,80],
  256.                                                speed=[[-3,+3],[-2,-1]],
  257.                                                num = num[0],
  258.                                                radius = particle_size,
  259.                                                burn_time = 1200,
  260.                                                timer=0))
  261.                     particles.append(SpurtFire(pos,
  262.                                                color=color["inside"],
  263.                                                time=[1,70],
  264.                                                speed=[[-2,+2],[-2,-1]],
  265.                                                num = num[1],
  266.                                                radius = particle_size,
  267.                                                burn_time = 1200,
  268.                                                timer=0))
  269.                     particles.append(SpurtFire(pos,
  270.                                                color=color["core"],
  271.                                                time=[1,50],
  272.                                                speed=[[-1,+1],[-2,-1]],
  273.                                                num = num[2],
  274.                                                radius = particle_size,
  275.                                                burn_time = 1200,
  276.                                                timer=0))

  277.                     
  278.                     


  279.             if event.type == KEYDOWN:
  280.                 if event.key == K_ESCAPE:
  281.                     pygame.quit()
  282.                     root.destroy()
  283.                     sys.exit()
  284.                     
  285.                 #F11切换全屏
  286.                 elif event.key == K_F11:
  287.                     fullscreen = not fullscreen
  288.                     screen_change = True

  289.                 elif event.key == K_1:
  290.                     bg = (255,255,255)

  291.                 elif event.key == K_2:
  292.                     bg = (0,0,0)

  293.                 elif event.key == K_o:
  294.                     # color,num,particle_size=thread01=Thread(target=set_up, name="线程1")
  295.                     if (if_start == 0):
  296.                         thread01=Thread(target=set_up, name="线程1")
  297.                         thread01.start()
  298.                         if_start = 1
  299.                     else:
  300.                         root.update()
  301.                         root.deiconify()
  302.                     pygame.event.clear()
  303.                     

  304.                 elif event.key == K_c:
  305.                     particles.clear()


  306.         #画背景
  307.         screen.fill(color["bg"])

  308.         #画 xxxx
  309.         for particle in particles:
  310.             particle.draw(screen)

  311.         #刷新
  312.         for particle in particles:
  313.             particle.update()
  314.             if not particle.burn_time:
  315.                 particles.remove(particle)

  316.         # 刷新界面
  317.         pygame.display.update()

  318.         

复制代码


使用多线程来解决这个办法

然后再打开还会保留上一次的数据,你自己写把内容清空就行了

如果对你有帮助的话,给个最佳答案呗!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-20 23:26:37 | 显示全部楼层
占楼
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-20 23:26:45 | 显示全部楼层
问题再86行,创建一个ttk窗口对象,从第二次开始就会出现问题,但第一次是好的(显示出现问题,好像不影响实际使用)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-20 23:27:22 | 显示全部楼层
cjjJasonchen 发表于 2023-7-20 23:26
问题再86行,创建一个ttk窗口对象,从第二次开始就会出现问题,但第一次是好的(显示出现问题,好像不影响 ...

之前用tkinter做的时候好像没有问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-20 23:29:07 | 显示全部楼层
[b]这是因为  style = ttk.Style()  创建了一个新的样式对象,但是没有加载任何主题。这就是为什么你第一次调用  set_up()  时看到的是你期望的主题(在你的主程序中已经加载了),但是在对  set_up()  的后续调用中,你看到的是默认主题:因为在这些情况下,你正在创建和使用一个新的、未加载主题的样式对象。

如果你将  style = ttk.Style()  行移动到  set_up()  函数的顶部(在任何其他 ttk 或 tkinter 代码之前),那么你应该会在  set_up()  的每次调用中都看到你期望的主题。你的  set_up()  函数应该如下所示:


  1. def set_up():
  2.     style = ttk.Style()
  3.     style.theme_use('default')
  4.     # ...
复制代码



这样,每次你创建一个新的样式对象时,你都会立即加载 default 主题,因此在你的 ttk 代码中你将始终看到这个主题,而不是 tkinter 的默认主题。
[/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-20 23:31:05 | 显示全部楼层
任务管理器:*************
这个是真的吃CPU 我都12核了,3个就卡的要死(巨形)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-20 23:33:12 | 显示全部楼层
isdkz 发表于 2023-7-20 23:29
这是因为  style = ttk.Style()  创建了一个新的样式对象,但是没有加载任何主题。这就是为什么你第一次调 ...

在第一次创建的时候就出现两个窗口,问题没有解决
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-20 23:37:03 | 显示全部楼层
歌者文明清理员 发表于 2023-7-20 23:31
任务管理器:*************
这个是真的吃CPU 我都12核了,3个就卡的要死(巨形)

这玩意这个状态下就是纯纯的只能看。。。要不然就要降低画质
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-21 08:11:59 From FishC Mobile | 显示全部楼层
建议不要切换黑白,直接换主题,正常主题是cosmo,黑色主题是darkly
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-21 09:45:17 | 显示全部楼层
liuhongrun2022 发表于 2023-7-21 08:11
建议不要切换黑白,直接换主题,正常主题是cosmo,黑色主题是darkly

切换黑白是切换的pygame(功能还没写),ttk的主题没有去动他
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-21 10:31:56 | 显示全部楼层
上去
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-21 11:55:06 | 显示全部楼层
Mike_python小 发表于 2023-7-20 23:23
使用多线程来解决这个办法

然后再打开还会保留上一次的数据,你自己写把内容清空就行了

问题解决了,非常感谢,但是楼主没有学过这个库,有没有推荐的教程看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-7-21 11:59:46 | 显示全部楼层
Mike_python小 发表于 2023-7-20 23:23
使用多线程来解决这个办法

然后再打开还会保留上一次的数据,你自己写把内容清空就行了

如果我点击右上角关闭窗口会报错欸
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-21 12:13:19 | 显示全部楼层
cjjJasonchen 发表于 2023-7-21 11:59
如果我点击右上角关闭窗口会报错欸

可以参考这个文章https://qa.1r1g.com/sf/ask/7780881/#google_vignette
然后再窗口关闭的时候替换成隐藏窗口的命令
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-7-21 12:13:57 | 显示全部楼层
cjjJasonchen 发表于 2023-7-21 11:55
问题解决了,非常感谢,但是楼主没有学过这个库,有没有推荐的教程看看

我这个说不好,学过好多教程,然后现在就是有啥不会的翻翻书就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 08:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表