首页 > 解决方案 > 无法隐藏根窗口

问题描述

我正在制作一个应用程序来捕获屏幕,在我的笔记本电脑中,它运行良好并在捕获之前隐藏了根窗口。

但是,在我朋友的笔记本电脑中,它仍然出现在屏幕截图中的根窗口,我root.withdraw()在我的代码中使用过。

from tkinter import *
import tkinter as tk
from tkinter import messagebox
from tkinter.filedialog import asksaveasfilename
# import messagebox from tkinter module
import tkinter.messagebox
from PIL import Image, ImageTk, ImageGrab, ImageEnhance  
root = tk.Tk()
root.title("Screenshot")
root.geometry('694x383')
root.resizable(0, 0)
bg = PhotoImage(file="bg_new.png")
root.tk.call('wm', 'iconphoto', root._w, PhotoImage(file='cameraico.png'))
# Show image using label
label1 = Label(root, image=bg)
label1.place(x=0, y=0)
def preview(image):
    win = tk.Toplevel()
    win.title("Preview")
    win.tk.call('wm', 'iconphoto', win.w, PhotoImage(file='cameraico.png'))
    win.image = ImageTk.PhotoImage(image)
    tk.Label(win, image=win.image).pack()
    win.grab_set()
    win.wait_window(win)

# capture fullscreen
def fullscreen():
    root.withdraw()
    roi_image = ImageGrab.grab(bbox=None)
    root.deiconify()
    save(roi_image)
def save(image):
    if image:
        # savepath = asksaveasfilename()
        savepath = asksaveasfilename(initialdir="/", title="Select file", filetypes=(
            ('JPEG', ('*.jpg', '*.jpeg', '*.jpe')), ("All Files", "*.*")))
        # roi_image.save(dialogue + ".jpg")
        if not savepath:
            tkinter.messagebox.showinfo("DTU Screenshot", "Your screenshot is not saved")
        else:
            image.save(savepath + ".jpg")
            tkinter.messagebox.showinfo("DTU Screenshot", "Your screenshot is saved")
            preview(image)

def area_sel():
    if var.get() == 1:
        fullscreen()
    elif var.get() == 2:
        x1 = y1 = x2 = y2 = 0
        roi_image = None
        def on_mouse_down(event):
            nonlocal x1, y1
            x1, y1 = event.x, event.y
            canvas.create_rectangle(x1, y1, x1, y1, outline='red', tag='roi')

        def on_mouse_move(event):
            nonlocal roi_image, x2, y2
            x2, y2 = event.x, event.y
            canvas.delete('roi-image')  # remove old overlay image
            roi_image = image.crop((x1, y1, x2, y2))  # get the image of selected region
            canvas.image = ImageTk.PhotoImage(roi_image)
            canvas.create_image(x1, y1, image=canvas.image, tag=('roi-image'), anchor='nw')
            canvas.coords('roi', x1, y1, x2, y2)
            # make sure the select rectangle is on top of the overlay image
            canvas.lift('roi')

        root.withdraw()  # hide the root window
        image = ImageGrab.grab()  # grab the fullscreen as select region background
        bgimage = ImageEnhance.Brightness(image).enhance(0.3)  # darken the capture image
        # create a fullscreen window to perform the select region action
        win = tk.Toplevel()
        win.attributes('-fullscreen', 1)
        win.attributes('-topmost', 1)
        canvas = tk.Canvas(win, highlightthickness=0)
        canvas.pack(fill='both', expand=1)
        tkimage = ImageTk.PhotoImage(bgimage)
        canvas.create_image(0, 0, image=tkimage, anchor='nw', tag='images')
        # bind the mouse events for selecting region
        win.bind('<ButtonPress-1>', on_mouse_down)
        win.bind('<B1-Motion>', on_mouse_move)
        win.bind('<ButtonRelease-1>', lambda e: win.destroy())
        # use Esc key to abort the capture
        win.bind('<Escape>', lambda e: win.destroy())
        # make the capture window modal
        win.focus_force()
        win.grab_set()  # prevents any interaction with the main window.
        win.wait_window(win)  # bỏ lệnh này nó sẽ bỏ qua lệnh 45 46 mà chạy lệnh 48
        root.deiconify()  # restore root window
        # save and show the capture image
        save(roi_image)


var = IntVar()
fulls_img = PhotoImage(file = 'fullscreen_new.png')
img_label = Label(image = fulls_img)
fulls_btn = Radiobutton(root,image = fulls_img, text="", variable=var, value=1,borderwidth =0,bg = '#c3d3f0').place(x = 610, y = 130 )
areas_img = PhotoImage(file = 'area_new.png')
img1_label = Label(image = areas_img)
area_btn = Radiobutton(root,image = areas_img, text="", variable=var, value=2,borderwidth =0,bg = '#c3d3f0').place(x = 610, y = 170 )
camera_img = PhotoImage(file = 'camera_new.png')
camera_label = Label(image = camera_img)
camera_btn = tk.Button(root, image =camera_img, text='', command=area_sel,borderwidth =0,bg = '#c3d3f0').place(x = 630, y = 210 )
root.mainloop()

我的笔记本电脑可以很好地运行此代码,所以我不知道为什么它们不能。

图片可以在这里找到。

标签: pythontkinterroot

解决方案


推荐阅读