简体   繁体   English

运行pythonw.exe时影响的Python脚本tkinter分辨率和屏幕截图大小

[英]Python script tkinter resolution and screenshot size effected when running pythonw.exe

I currently have a python script which launches a tkinter GUI instance, with a button that once clicked takes a screenshot. 我目前有一个python脚本,可启动tkinter GUI实例,并带有一个按钮,单击该按钮即可截取屏幕截图。

When I run this script under python.exe, the tkinter resolution is fine and the screenshot captures the whole screen. 当我在python.exe下运行此脚本时,tkinter分辨率很好,并且屏幕截图捕获了整个屏幕。 However, when using pythonw.exe, the tkinter window resolution changes (button gets bigger for example) and the screenshot only captures a portion of the screen - the top left hand corner normally. 但是,使用pythonw.exe时,tkinter窗口的分辨率会发生变化(例如,按钮变大),并且屏幕截图仅捕获屏幕的一部分-通常位于左上角。

I need to use pythonw.exe in order to prevent the console window appearing. 我需要使用pythonw.exe来防止出现控制台窗口。

Does anyone know why the tkinter window resolution and the screenshot capture is being effected? 有谁知道为什么会影响tkinter窗口分辨率和屏幕截图? Presumably the effect on the resolution is why the screenshot capture is being reduced as well. 大概对分辨率的影响就是为什么截图捕获量也会减少。

I am fairly new to Python, so any help with this would be greatly appreciated, below is snippet of the code for the tkinter window and the screenshot functionality. 我对Python相当陌生,因此,对此的任何帮助将不胜感激,以下是tkinter窗口和屏幕截图功能的代码片段。 To reiterate this functionality runs completely fine under python.exe. 要重申此功能,请在python.exe下完全正常运行。

The screenshot functionality using ImageGrab: 使用ImageGrab的屏幕截图功能:

    callback1():
         ImageGrab.grab_to_file('test.png')

The tkinter window: tkinter窗口:

    master = Tk()  
    master.wm_attributes("-topmost", 1)
    master.title("Report")
    master.configure(background='white')
    master.iconbitmap(default='icon.ico')
    master.resizable(0, 0)
    frame1 = Frame(master, bg='white')
    frame1.pack(side=BOTTOM, fill=X)
    button1 = Button(frame1, compound=BOTTOM, width=307, height=82,
    image=photo1, bg='white', command=callback1)
    button1.pack(side=TOP, padx=2, pady=8)

I have now fixed this. 我现在已经解决了这个问题。 It seems it was related to the compatibility settings for pythonw.exe in Windows. 似乎与Windows中pythonw.exe的兼容性设置有关。 Changing the following fixes the image capture, but also the tkinter window resolution: 更改以下内容可以修复图像捕获,也可以修复tkinter窗口分辨率:

  • Go to your python directory ( c:/python27/ for me) 转到您的python目录(对我来说c:/python27/
  • Right click python.exe and select properties 右键单击python.exe并选择属性
  • Select the compatibility tab 选择兼容性选项卡
  • Press the "Change settings for all users" button 按下“更改所有用户的设置”按钮
  • Check the "Disable display scaling on high DPI settings" box 选中“在高DPI设置上禁用显示比例缩放”

Credited by this post: 归功于这篇文章:

Python Imaging Library fails to grab whole screen Python Imaging Library无法捕获整个屏幕

Hopefully this helps someone with the same issues. 希望这可以帮助遇到相同问题的人。 It does beg the question as to how this can be done automatically, as for users of a python application it is not user-friendly for them to have to change these settings. 它确实引起了关于如何自动完成此操作的问题,因为对于python应用程序的用户来说,他们不得不更改这些设置并不友好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM