首页 > 解决方案 > 导入 pyautogui 会改变 tkinter 的大小

问题描述

我有一个 tkiner 框架,里面有几个按钮。我想将这些按钮之一绑定到 pyautogui 脚本,但是导入但不实现 pyautogui 的行为会改变我所有按钮、复选框和选项菜单的大小。它发生在此窗口和所有子窗口中。

为什么会发生这种情况,我怎样才能让我的按钮保持相同的大小?下面的代码复制了这个问题。

from tkinter import *
#import pyautogui # comment out to see normal sized 
                  # buttons, include to see issue

class mywindow(Frame):
    def __init__(self, master, *pargs):
        Frame.__init__(self, master, *pargs)
        master.overrideredirect(1)
        self.frame = Frame(master, width=50, height=200,
                           borderwidth=2, relief=RAISED)
        self.frame.grid(row=0,column=0)

        self.bg = Label(self.frame, bg='gray30', width=50, height=200, anchor=E)
        self.bg.pack()

        self.mybutton1 = Button(self.bg, text="my text",
                            command=self.mycommand)
        self.mybutton1.grid(row=0,sticky=W+E+N+S)

        self.mybutton2 = Button(self.bg, text="my text",
                            command=self.mycommand)
        self.mybutton2.grid(row=1,sticky=W+E+N+S)

        self.mybutton3 = Button(self.bg, text="my text",
                            command=self.mycommand)
        self.mybutton3.grid(row=2,sticky=W+E+N+S)

    def mycommand(self):
        pass

def center_window(root, width, height):
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    x = (screen_width/2) - (width)
    y = (screen_height/2) - (height)
    root.geometry('%dx%d+%d+%d' % (width, height, x, y))

def go():
    root = Tk()
    center_window(root, 70, 110)
    e = mywindow(root)
    root.mainloop()


if __name__ == '__main__':
    go()

标签: pythonpython-3.xtkinterpyautogui

解决方案


推荐阅读