【问题标题】:Change image of button when pressed - Python 3.7 Tkinter按下时更改按钮的图像 - Python 3.7 Tkinter
【发布时间】:2020-02-15 16:43:50
【问题描述】:

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

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

除此之外,我还想知道如何为整个窗口设置背景,我尝试了画布但似乎没有用。我还想在显示“屏幕摄像师”的标签旁边放置一个徽标,如果可以的话,我可以知道如何对齐它。

下面是代码:-

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()

谢谢!!

【问题讨论】:

    标签: python-3.x tkinter imagebutton


    【解决方案1】:

    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)如果要改变整个窗口的背景颜色,需要改变根窗口的background选项,根窗口内的框架和框架内的标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-06
      • 1970-01-01
      • 2011-01-11
      • 2013-06-25
      • 2019-01-04
      相关资源
      最近更新 更多