python winfo__Tk()、Toplevel()和winfo_Toplevel()。它们之间的区别以及如何和何时有效使用?...

本文探讨了在 Python 的 tkinter 模块中,winfo_Tk、Toplevel 和 winfo_toplevel 之间的区别。主要内容包括:Tk() 实例的自动创建,Toplevel() 用于创建附加窗口,以及 winfo_toplevel() 方法用于获取顶级窗口引用。错误使用可能导致不必要的窗口创建和销毁。通过示例代码,解释了如何正确地使用这些组件以避免问题。
摘要由CSDN通过智能技术生成

On running above code "Root" widget is getting created. On closing "Root", two widgets are created with one titled "Window widget" and

other being unwanted. On closing unwanted widget, "Window widget" is

also getting destroyed. What is actually happening here and how to

overcome?

当您在没有实际Tk()实例的情况下创建任何小部件时,会自动创建一个Tk()实例,从而在第一个代码段的第二部分运行时产生不需要的顶层小部件。另外,当在缺少master选项的情况下创建小部件时,假设该实例是其中一个Tk实例的子实例,在上述情况下,只有一个实例,这是自动创建的实例。当父级被销毁时,它下面的所有小部件也会被销毁,因此当您关闭不需要的小部件(即Tk的实例)时,该Toplevel实例也会在其父级被销毁时被销毁。

在第二部分中,^{}再次引用自动创建的Tk实例,并使用自动创建的Tk作为父实例创建其他子实例,这在技术上应该很好,但作为代码维护起来会比创建相同GUI的标准方法更困难。winf = winfo_Toplevel()

winf.title("Winfo Widget")

winf.mainloop()

在上面的代码中,除非导入或以其他方式定义,winfo_Toplevel没有任何意义,首先,它与winfo_toplevel不同,因为python是区

用黄色方格代表蛇身,简易的小游戏。原文件:#!/usr/bin/python from Tkinter import * import random class snake(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.body = [(0,0)] self.bodyid = [] self.food = [ -1, -1 ] self.foodid = -1 self.gridcount = 10 self.size = 500 self.di = 3 self.speed = 500 self.top = self.winfo_toplevel() self.top.resizable(False, False) self.grid() self.canvas = Canvas(self) self.canvas.grid() self.canvas.config(width=self.size, height=self.size,relief=RIDGE) self.drawgrid() s = self.size/self.gridcount id = self.canvas.create_rectangle(self.body[0][0]*s,self.body[0][1]*s, (self.body[0][0]+1)*s, (self.body[0][1]+1)*s, fill="yellow") self.bodyid.insert(0, id) self.bind_all("<KeyRelease>", self.keyrelease) self.drawfood() self.after(self.speed, self.drawsnake) def drawgrid(self): s = self.size/self.gridcount for i in range(0, self.gridcount+1): self.canvas.create_line(i*s, 0, i*s, self.size) self.canvas.create_line(0, i*s, self.size, i*s) def drawsnake(self): s = self.size/self.gridcount head = self.body[0] new = [head[0], head[1]] if self.di == 1: new[1] = (head[1]-1) % self.gridcount elif self.di == 2: new[0] = (head[0]+1) % self.gridcount elif self.di == 3: new[1] = (head[1]+1) % self.gridcount else: new[0] = (head[0]-1) % self.gridcount next = ( new[0], new[1] ) if next in self.body: exit() elif next == (self.food[0], self.food[1]): self.body.insert(0, next) self.bodyid.insert(0, self.foodid) self.drawfood() else: tail = self.body.pop() id = self.bodyid.pop() self.canvas.move(id, (next[0]-tail[0])*s, (next[1]-tail[1])*s) self.body.insert(0, next) self.bodyid.insert(0, id) self.after(self.speed, self.drawsnake) def drawfood(self): s = self.size/self.gridcount x = random.randrange(0, self.gridcount) y = random.randrange(0, self.gridcount) while (x, y) in self.body: x = random.randrange(0, self.gridcount) y = random.randrange(0, self.gridcount) id = self.canvas.create_rectangle(x*s,y*s, (x+1)*s, (y+1)*s, fill="yellow") self.food[0] = x self.food[1] = y self.foodid = id def keyrelease(self, event): if event.keysym == "Up" and self.di != 3: self.di = 1 elif event.keysym == "Right" and self.di !=4: self.di = 2 elif event.keysym == "Down" and self.di != 1: self.di = 3 elif event.keysym == "Left" and self.di != 2: self.di = 4 app = snake() app.master.title("Greedy Snake") app.mainloop()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值