Python 在 Tkinter 窗口中更改标签位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15675469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:40:48  来源:igfitidea点击:

Change position of label in Tkinter window

pythontkinter

提问by lukeanders70

I am writing a simple program that pulls up an image (BackgroundFinal.png) and displays it in a window. I want to be able to press a button on the window to move the picture down by 22 pixels. Everything works except the button does not do anything.

我正在编写一个简单的程序,用于拉出图像 (BackgroundFinal.png) 并将其显示在窗口中。我希望能够按下窗口上的按钮将图片向下移动 22 像素。一切正常,除了按钮不做任何事情。

import Tkinter
import Image, ImageTk
from Tkinter import Button


a = 0       #sets inital global 'a' and 'b' values
b = 0

def movedown():             #changes global 'b' value (adding 22)
    globals()[b] = 22
    return

def window():               #creates a window 
    window = Tkinter.Tk();
    window.geometry('704x528+100+100');

    image = Image.open('BackgroundFinal.png');      #gets image (also changes image size)
    image = image.resize((704, 528));
    imageFinal = ImageTk.PhotoImage(image);

    label = Tkinter.Label(window, image = imageFinal);   #creates label for image on window 
    label.pack();
    label.place(x = a, y = b);      #sets location of label/image using variables 'a' and 'b'

    buttonup = Button(window, text = 'down', width = 5, command = movedown()); #creates button which is runs movedown()
    buttonup.pack(side='bottom', padx = 5, pady = 5);

    window.mainloop();

window()

If I am not mistaken, the button should change the global 'b' value, therefore changing the y position of the label. I really appreciate any help, sorry for my god-awful conventions. Thanks in advance!

如果我没记错的话,按钮应该更改全局“b”值,从而更改标签的 y 位置。我真的很感谢任何帮助,为我糟糕的约定感到抱歉。提前致谢!

采纳答案by lukeanders70

Thanks for the reply but, It was not really what I was looking for. I'll post what I found worked best here for anybody else with the same problem.

感谢您的回复,但这并不是我真正想要的。我会发布我发现的最适合其他有同样问题的人。

Essentially, It is much better, in this case, to use a Canvas instead of a label. With canvases, you can move objects with canvas.move, here is a simple example program

本质上,在这种情况下,使用 Canvas 而不是标签要好得多。使用画布,您可以使用 canvas.move 移动对象,这里是一个简单的示例程序

# Python 2
from Tkinter import *

# For Python 3 use:
#from tkinter import *

root = Tk()
root.geometry('500x500+100+100')

image1 = PhotoImage(file = 'Image.gif')

canvas = Canvas(root, width = 500, height = 400, bg = 'white')
canvas.pack()
imageFinal = canvas.create_image(300, 300, image = image1)

def move():
    canvas.move(imageFinal, 0, 22)  
    canvas.update()

button = Button(text = 'move', height = 3, width = 10, command = move)
button.pack(side = 'bottom', padx = 5, pady = 5)

root.mainloop()

my code may not be perfect (sorry!) but that is the basic idea. Hope I help anybody else with this problem

我的代码可能并不完美(抱歉!)但这是基本思想。希望我能帮助其他人解决这个问题

回答by mgilson

You have a few problems here.

你在这里有一些问题。

First, you're using packand place. In general, you should only use 1 geometry manager within a container widget. I don't recommend using place. That's just too much work that you need to manage.

首先,您正在使用packplace。通常,您应该只在容器小部件中使用 1 个几何管理器。我不建议使用place. 您需要管理的工作太多了。

Second, you're calling the callback movedownwhen you construct your button. That's not what you want to do -- You want to pass the function, not the result of the function:

其次,您在movedown构造按钮时调用回调。这不是你想要做的——你想传递函数,而不是函数的结果:

buttonup = Button(window, text = 'down', width = 5, command = movedown)

Third, globalsreturns a dictionary of the current namespace -- It's not likely to have an integer key in it. To get the reference to the object referenced by b, you'd need globals()["b"]. Even if it did, changing the value of bin the global namespace won't change the position of your label because the label has no way of knowing that change. And in general, if you needto use globals, you probablyneed to rethink your design.

第三,globals返回当前命名空间的字典——其中不可能有整数键。要获得对被 引用的对象的引用b,您需要globals()["b"]. 即使这样做了,更改b全局命名空间中的值也不会更改标签的位置,因为标签无法知道该更改。一般来说,如果您需要使用globals,您可能需要重新考虑您的设计。

Here's a simple example of how I would do it...

这是我将如何做的一个简单示例...

import Tkinter as tk

def window(root):
    buf_frame = tk.Frame(root,height=0)
    buf_frame.pack(side='top')
    label = tk.Label(root,text="Hello World")
    label.pack(side='top')
    def movedown():
        buf_frame.config(height=buf_frame['height']+22)

    button = tk.Button(root,text='Push',command=movedown)
    button.pack(side='top')

root = tk.Tk()
window(root)
root.mainloop()