2

What is the primary difference between these two functions?

I'm currently working on a program that contains a canvas and a label, the latter is behaving as a button. Now the canvas was placed using .pack() while I used .place() for the label. Additionally the program reads and uses the location of mouse clicks to perform certain actions.

There is currently a bug that causes the program to trigger an unexpected action upon clicking the label, to my surprise upon clicking the label the x and y coordinate of the mouse click are not in reference to the window (as I expected it to be) but rather to the top-left coordinates of the label.

Is there a way to set it so that the label does not modify the x and y coordinate of the mouse click within it?

I'm relatively new to python and this is the first projects I've made using tkinter. If needed I can provide the code upon request.

EDIT: Entire program can be found here. Labels are added starting at line 87:

      newgame_Label = Label(parent, anchor=NW, font="Calibri",
           text="New Game")
      newgame_Label.bind("<Button 1>", self.newgame_Click)      
      newgame_Label.place(x=450, y=300)

      reset_Label = Label(parent, anchor=NW, font="Calibri",
           text="Reset Board")
      reset_Label.bind("<Button 1>", self.reset_Click)      
      reset_Label.place(x=450, y=400)  

The function initBoard calls the .pack() method on the canvas. The program is a clone of Light's Out, thought it would be a good way of learning python and tkinter.

Alternately I can just not use a label and simply use the create_text method available to a canvas widget, but this would require some workaround to determine if it has been clicked. Mostly interested in finding out why is it that .place() for a Label Widget behaves the way it does event though they share the same frame.

4

2 回答 2

5

主要区别在于place允许您指定绝对坐标,并pack使用允许您沿框边放置小部件的框模型。

place 在现实世界的应用程序中并不经常使用。它非常适合解决一小部分问题,但 pack 和 grid 更有用。Place 要求您花费更多的精力来正确调整 GUI 大小。

于 2013-07-12T16:43:03.500 回答
0

您可以一起使用 pack() 和 place() :

r = Tk()
label = Label(r, text = any text)
label.pack()
label.place(x = x, y = y)

记住 !您必须在放置前使用包装

于 2020-06-23T05:10:48.980 回答