鱼C论坛

 找回密码
 立即注册
查看: 3194|回复: 3

[已解决]tkinter的mainloop使用

[复制链接]
发表于 2018-3-30 09:39:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看第15章TKINTER的时候,15.1章例子中使用mainloop时是:
root.mainloop()
备注:root = tkinter.Tk()

之后章节关于Label和Button例子中,mainloop调用时却没有root前缀了:
mainloop()

什么时候需要root.什么时候不需要或者可省缺?

谢谢!

相关代码如下:

#p15_1.py=============basic
import tkinter as tk

root = tk.Tk()
root.title("FC Demo")

theLabel = tk.Label(root,text='我的第二个窗口程序!')
theLabel.pack()

root.mainloop()

#p15_3.py=============Label(image)
from tkinter import *

root = Tk()
textLabel = Label(root,\
text='hello world!\nThis is Lable test.',justify=LEFT,padx=10)
textLabel.pack(side=LEFT)
photo=PhotoImage(file='t2.gif')
imgLabel=Label(root,image=photo)
imgLabel.pack(side=RIGHT)

mainloop()

最佳答案
2018-3-30 12:44:42
mainloop定义于于tkinter模块下,

所以你的第一个root.mainloop()
也可以写成tk.mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-3-30 09:46:26 | 显示全部楼层
2018-03-30_094601.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-30 12:44:42 | 显示全部楼层    本楼为最佳答案   
mainloop定义于于tkinter模块下,

所以你的第一个root.mainloop()
也可以写成tk.mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-30 16:33:33 | 显示全部楼层
塔利班 发表于 2018-3-30 12:44
mainloop定义于于tkinter模块下,

所以你的第一个root.mainloop()

谢谢!
我试了一下类似的情况,from...import改成直接import后如下:
以下运行正常,要加前缀的地方确实多了很多
from...import比较好用

import tkinter as tk

root = tk.Tk()
GIRLS = ['西施','王昭君','貂蝉','杨玉环']
v = []
for girl in GIRLS:
        v.append(tk.IntVar())
        b = tk.Checkbutton(root,text=girl,variable=v[-1])
        b.pack(anchor=tk.SW)

root.mainloop()
#也可以tk.mainloop()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-14 02:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表