Python Tkinter是否在GPU上渲染?如果不是,如何在GPU上渲染
简单地说,我试着在tkinter中制作一个游戏,它运行得很慢,我可以对它进行优化,但如果它在CPU上渲染,我无法真正改进它。我的CPU很慢。如果有人能帮我在GPU上渲染,那会有帮助:DPython Tkinter是否在GPU上渲染?如果不是,如何在GPU上渲染,python,tkinter,Python,Tkinter,简单地说,我试着在tkinter中制作一个游戏,它运行得很慢,我可以对它进行优化,但如果它在CPU上渲染,我无法真正改进它。我的CPU很慢。如果有人能帮我在GPU上渲染,那会有帮助:D from tkinter import * #importing libraries import random import time import sys import keyboard from PIL import ImageTk, Image tk = Tk() class map: size
from tkinter import * #importing libraries
import random
import time
import sys
import keyboard
from PIL import ImageTk, Image
tk = Tk()
class map:
size = 1000, 1000 # setting map values
class camera:
xposition = 0 #setting camera variables#
yposition = 0
effect = 0
width = 500
height = 500
game = Canvas(tk, width = camera.width, height = camera.height) #creating the window
game.pack()
bob = game.create_rectangle(0, 0, 70, 70,) #creating 'BOB' (test subject)
RedBerryBushTexture = ImageTk.PhotoImage(Image.open ('BerryBush1.png')) #importing bush image texture
Bush0 = game.create_image(50, 90, anchor = NW, image = RedBerryBushTexture)
Bush1 = game.create_image(432, 325, anchor = NW, image = RedBerryBushTexture) #creating a few bushes
Bush2 = game.create_image(482, 312, anchor = NW, image = RedBerryBushTexture)
Bush3 = game.create_image(212, 341, anchor = NW, image = RedBerryBushTexture)
Bush4 = game.create_image(111, 111, anchor = NW, image = RedBerryBushTexture)
while True:
try:
tk.title("The Phoenix Game") #making the window
game.update()
if keyboard.is_pressed('w'): #registering W key
game.move('all', 0, 15)
camera.xposition = camera.xposition + 15
print(camera.xposition, camera.yposition)
if keyboard.is_pressed('s'): #registering S key
game.move('all', 0, -15)
camera.xposition = camera.xposition - 15
print(camera.xposition, camera.yposition)
if keyboard.is_pressed('a'): #regestering A key
game.move('all', 15, 0)
camera.yposition = camera.yposition + 15
print(camera.xposition, camera.yposition)
if keyboard.is_pressed('d'): #regestering D key
game.move('all', -15, 0)
camera.yposition = camera.yposition - 15
print(camera.xposition, camera.yposition)
time.sleep(0.017)
except:
print(" Error With Rendering. Quitting. (Error code 1)") # displaying render error message
time.sleep(1)
sys.exit()
print(" BOOTING UP GAME") #displaying startup message
在使用
tkinter
时,不应使用while True
循环和键盘
模块。查看tkinter
脚本和tkinter
绑定后的。这应该会让你的程序运行得更快,而不是让你的cpu崩溃。我的cpu仍然非常慢,尽管它有8个内核。对于FX8300,我可能还需要使用多线程使用threading
和tkinter
不是一个好主意。有时,如果从另一个线程调用tkinter
,它可能会崩溃,而不会出现错误回溯。另外,由于python的设计方式(python的GIL),它无法利用所有8个核心,除非您进行一些欺骗。我建议您停止在循环时使用,同时也停止使用键盘
模块。这会给你的计划带来足够好的种子动力