Tkinker框架与包

时间:2017-01-24 03:26:45

标签: python tkinter

我只和tkinter合作了几个月。我写了一些代码 使用'Frame'和'Grid'没有问题。我刚开始尝试使用'包' 并且我遇到此错误的问题:AttributeError:'Frame'对象没有属性'frame1'。

我做错了什么?

代码:

def generate_permutations(nums, res, l, r):
        if l == r:
            res.append(nums[:])        # <=== change
        for i in range(l, r+1):
            nums[i], nums[l] = nums[l], nums[i]
            generate_permutations(nums, res, l+1, r)
            nums[l], nums[i] = nums[i], nums[l]
In [158]: res=[];nums=[1,2,3]
In [159]: generate_permutations(nums, res,0,2)
In [160]: res
Out[160]: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]]

回溯:

  

追踪(最近一次通话):     文件“./tkerr3.py”,第7行,in       frame1 = Frame(root).frame1.pack(side = LEFT,fill = BOTH)   AttributeError:'Frame'对象没有属性'frame1'

1 个答案:

答案 0 :(得分:2)

您似乎对如何调用pack有错误的想法。

frame1 = Frame(root).frame1.pack(....)

Frame对象没有属性frame1,而是一旦Frame对象绑定到您的变量,您就想在下一行调用pack。

frame1 = Frame(root)
frame1.pack(....)

所有其他'打包'小部件也一样。

你还有一个错字等待

labe11=Label(frame1).labeL1.pack()

成功

label1=Label(frame1)
label1.pack()

这里代码的基本修正版本,回调被删除。

from tkinter import *

def dummy(): pass

root=Tk()
root.title("Spin Box")
root.resizable(0,0)

frame1=Frame(root)
frame1.pack(side=LEFT, fill=BOTH)
frame2=Frame(root)
frame2.pack(side=RIGHT, fill=BOTH)

labe11=Label(frame1)
labe11.pack()
label2=Label(frame2, text="How many Ticket?", font="bold")
label2.pack()

button1=Button(frame2, text="Get Tickets", font="bold", command=dummy)
button1.pack()
button2=Button(frame2, text=" Close ", font="bold", command=dummy)
button2.pack()

sp1=Spinbox(frame2, from_='1', to='5', bd=2, bg="white", state='readonly', relief=SUNKEN)
sp1.pack()

root.mainloop()
相关问题