按下时更改按钮的图像 - Python 3.7 Tkinter

Change image of button when pressed - Python 3.7 Tkinter

这似乎是一个愚蠢的问题,但在 Whosebug 上找不到答案,我的项目需要这个。希望有人能帮忙。

现在,我正在创建跨平台screen recorder。我在下面附上了当前的 UI。在下面 UI 中,我希望暂停按钮变为播放按钮(交换图像),或者如果再次按下则反之亦然。我在这里附上了整个 GUI 代码。请帮我解决这个问题。我在 Ubuntu OS 上使用 python 3.7,但它应该适用于所有平台。

除此之外,我还想知道如何为整个 window 设置背景,我尝试了 canvas 但似乎没有用。我还想在显示 "Screen Videographer" 的标签旁边放置一个徽标,是否可能,如果可以,我可以知道如何对齐它。

下面是代码:-

from tkinter import *
from tkinter.ttk import *
from detect_screen_size import detect_screen_size
from PIL import Image, ImageTk
import tkinter as tk

paused= True

LOGO_PNG_PATH="pic/logo.png"
LOGO_PATH="pic/logo.ico"
BG_PATH = 'pic/bg_1.jpg'
LOGO_LINUX_PATH="@pic/logo_1.xbm"


def play():
    global paused,image2
    print("start of function",paused)
    if paused:
        image2=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/play.png")
        paused=False
        print("inside if function",paused)
    else:
        image2 = PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/symbol.png")
        paused=True
        print("inside if function", paused)

def pause():
    print("button hit")
def stop():
    print("button stop")
def record():
    print("button rec")
WIDTH, HEIGHT = 350, 100

root = Tk()
#root.geometry('{}x{}'.format(WIDTH, HEIGHT))
root.resizable(0, 0)
root.style = Style()
#('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
root.style.theme_use("clam")
if detect_screen_size().detect_os()=="Linux":
    root.iconbitmap(LOGO_LINUX_PATH)
else:
    root.iconbitmap(default=LOGO_PATH)
root.title("Screenvideographer")


frame_display = tk.Frame(relief=FLAT)
label = tk.Label(master=frame_display, text="Screen Videographer")
label.pack()
canvas = Canvas(root, width = 50, height = 50)
canvas.pack(side=LEFT)
img = PhotoImage(file=LOGO_PNG_PATH)
canvas.create_image(50,50, anchor=NW, image=img)


frame_display.pack()
frame_controls = tk.Frame(relief=FLAT)

image1=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/record.png")
image2=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/symbol.png")
image3=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/stop.png")



Button(master=frame_controls,width=35,image=image1,command=record).pack(side=LEFT,padx=7,pady=4)
Button(master=frame_controls,width=35,image=image2,command=play).pack(side=LEFT,padx=7,pady=4)
Button(master=frame_controls,width=35,image=image3,command=stop).pack(side=RIGHT,padx=7,pady=4)


frame_controls.pack()


root.mainloop()

谢谢!!

1) 您应该在程序启动时加载 4 个图像:

img_record=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/record.png")
img_pause=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/symbol.png")
img_stop=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/stop.png")
img_play=PhotoImage(file="/media/admin_ash/New Volume/proj/screen rec/pic/gui_controls/play.png")

2) 您需要为 play/pause 按钮分隔 Button(...)pack(...) 语句:

Button(master=frame_controls,width=35,image=img_record,command=record).pack(side=LEFT,padx=7,pady=4)
play_btn = Button(master=frame_controls,width=35,image=img_play,command=play)
play_btn.pack(side=LEFT,padx=7,pady=4)
Button(master=frame_controls,width=35,image=img_stop,command=stop).pack(side=RIGHT,padx=7,pady=4)

3) 根据当前播放状态更改按钮图片:

def play():
    global paused
    print("start of function", paused)
    if paused:
        play_btn.config(image=img_pause)
        paused = False
        print("inside if function", paused)
    else:
        play_btn.config(image=img_play)
        paused = True
        print("inside if function", paused)

4) 编辑 stop() 函数以改回 play 图像:

def stop():
    global paused
    print("button stop")
    if not paused:
        play_btn.config(image=img_play)
        paused = True

5) 如果你想在 Screen Videographer 标签旁边显示图片,只需使用标签的 imagecompound 选项:

# logo = PhotoImage(file="/path/to/logo/image")
label = tk.Label(master=frame_display, image=logo, text="Screen Videographer", compound="left")

6)如果要改变整个window的背景颜色,需要改变rootwindow的background选项,frames inside rootwindow ] 和框架内的标签。