[英]what happens when you define a function?
我在 Stackoverflow 上找到了一个用于从实际 window 制作片段的漂亮脚本,我很想在我正在创建的个人应用程序中使用它。 但我认为我在这里遗漏了一些重要的东西,我不明白为什么它不起作用。
from tkinter import *
import pyautogui
import datetime
class Application():
def __init__(self, master):
self.master = master
self.rect = None
self.x = self.y = 0
self.start_x = None
self.start_y = None
self.curX = None
self.curY = None
# root.configure(background = 'red')
# root.attributes("-transparentcolor","red")
root.attributes("-transparent", "blue")
root.geometry('400x50+200+200') # set new geometry
root.title('Lil Snippy')
self.menu_frame = Frame(master, bg="blue")
self.menu_frame.pack(fill=BOTH, expand=YES)
self.buttonBar = Frame(self.menu_frame,bg="")
self.buttonBar.pack(fill=BOTH,expand=YES)
self.snipButton = Button(self.buttonBar, width=3, command=self.createScreenCanvas, background="green")
self.snipButton.pack(expand=YES)
self.master_screen = Toplevel(root)
self.master_screen.withdraw()
self.master_screen.attributes("-transparent", "blue")
self.picture_frame = Frame(self.master_screen, background = "blue")
self.picture_frame.pack(fill=BOTH, expand=YES)
def takeBoundedScreenShot(self, x1, y1, x2, y2):
im = pyautogui.screenshot(region=(x1, y1, x2, y2))
x = datetime.datetime.now()
fileName = x.strftime("%f")
im.save("snips/" + fileName + ".png")
def createScreenCanvas(self):
self.master_screen.deiconify()
root.withdraw()
self.screenCanvas = Canvas(self.picture_frame, cursor="cross", bg="grey11")
self.screenCanvas.pack(fill=BOTH, expand=YES)
self.screenCanvas.bind("<ButtonPress-1>", self.on_button_press)
self.screenCanvas.bind("<B1-Motion>", self.on_move_press)
self.screenCanvas.bind("<ButtonRelease-1>", self.on_button_release)
self.master_screen.attributes('-fullscreen', True)
self.master_screen.attributes('-alpha', .3)
self.master_screen.lift()
self.master_screen.attributes("-topmost", True)
def on_button_release(self, event):
self.recPosition()
if self.start_x <= self.curX and self.start_y <= self.curY:
print("right down")
self.takeBoundedScreenShot(self.start_x, self.start_y, self.curX - self.start_x, self.curY - self.start_y)
elif self.start_x >= self.curX and self.start_y <= self.curY:
print("left down")
self.takeBoundedScreenShot(self.curX, self.start_y, self.start_x - self.curX, self.curY - self.start_y)
elif self.start_x <= self.curX and self.start_y >= self.curY:
print("right up")
self.takeBoundedScreenShot(self.start_x, self.curY, self.curX - self.start_x, self.start_y - self.curY)
elif self.start_x >= self.curX and self.start_y >= self.curY:
print("left up")
self.takeBoundedScreenShot(self.curX, self.curY, self.start_x - self.curX, self.start_y - self.curY)
self.exitScreenshotMode()
return event
def exitScreenshotMode(self):
print("Screenshot mode exited")
self.screenCanvas.destroy()
self.master_screen.withdraw()
root.deiconify()
def exit_application(self):
print("Application exit")
root.quit()
def on_button_press(self, event):
# save mouse drag start position
self.start_x = self.screenCanvas.canvasx(event.x)
self.start_y = self.screenCanvas.canvasy(event.y)
self.rect = self.screenCanvas.create_rectangle(self.x, self.y, 1, 1, outline='red', width=3, fill="blue")
def on_move_press(self, event):
self.curX, self.curY = (event.x, event.y)
# expand rectangle as you drag the mouse
self.screenCanvas.coords(self.rect, self.start_x, self.start_y, self.curX, self.curY)
def recPosition(self):
print(self.start_x)
print(self.start_y)
print(self.curX)
print(self.curY)
if __name__ == '__main__':
root = Tk()
app = Application(root)
root.mainloop()
代码正在使用:
if __name__ == '__main__':
root = Tk()
app = Application(root)
root.mainloop()
代码也正在使用
root = Tk()
app = Application(root)
root.mainloop()
但是为什么不这样呢???
def main():
root = Tk()
app = Application(root)
root.mainloop()
main()
从 class 应用程序中获取错误:
...root.attributes("-transparent", "blue")
NameError: name 'root' is not defined
这是因为
def main():
root = Tk()
app = Application(root)
root.mainloop()
在 function 的本地 scope 中定义root
,因此在Application
中引用它时会导致 NameError。 修复它尝试在 class 中使用self.master
而不是root
发生的情况是您在 function main
命名空间中创建根; 因此,它不再可以在它之外访问。
你可以这样做:
def main():
global root # this ensures you use the already created root from the `__main__` namespace
root = Tk()
app = Application(root)
root.mainloop()
if __name__ == '__main__':
root = None
main()
有关scope 和命名空间的更多信息
我想说试试
def main():
root = Tk()
app = Application(root)
root.mainloop()
main()
在您呼叫其他 1 或 2 之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.