在pycharm中的tkinter窗體中加載背景音樂的方法,代碼如下:
import tkinter as tk #窗體
import pygame #音樂子產品
root=tk.Tk() #定義一個tkinter類tk; Tkinter為圖形界面庫
root.title("随機點名") #标題
root.geometry('550x450') #窗體尺寸
#定義圖示簽位
img=r'F:\PythonFiles\PycharmFile\exc.gif'
photo = tk.PhotoImage(file=img)
labelImg = tk.Label(root,image = photo) label.image = photo
labelImg.pack()
#背景音樂
pygame.init()
music = pygame.mixer.music.load(r'F:\PythonFiles\PycharmFile\tante.mp3')
pygame.mixer.music.play(-1, 10)
#screen = pygame.display.set_mode((800, 600)) #音樂視窗是否顯示
root.mainloop() #使視窗保持