如何在tkinter中移动物体

huangapple go评论64阅读模式
英文:

How to move things in tkinter

问题

我一整天都在尝试学习tkinter,但毫无结果。我找到的所有信息都似乎已经过时了,我看到了一千种移动物件的方法,但它们都固定在一侧。我写CSS的时间比这还要短。

from tkinter import *
from tkinter import ttk
root = Tk()
root.title('aaaa?')
root.geometry('400x300')
root.config(bg='#F2B33D')
frm = Frame(root, bg='#F2B33D')
frm.grid()
Label(frm, text="Hello World!").grid(column=0, row=0, ipadx=100)
Button(frm, text="Not yet").grid(column=0, row=3)
Button(frm, text="Quit", command=root.destroy).grid(column=2, row=3)

frm.pack(expand=True)
root.mainloop()

我想要做的是使用这个失败的代码在底部中心对齐按钮,而在顶部中心对齐标签,然后知道用户是否点击了“Not yet”按钮,并将其存储在变量中。我甚至制作了一个很酷的绘图。

图片
谢谢。

英文:

i have been all day trying to learn tkinter but to no avail. All the information I found seems outdated and I have seen a thousand methods to move things but all of them stick to one side. I have written css in less time than this.

from tkinter import *
from tkinter import ttk
root = Tk()
root.title('aaaa?')
root.geometry('400x300')
root.config(bg='#F2B33D')
frm = Frame(root, bg='#F2B33D')
frm.grid()
Label(frm, text="Hello World!").grid(column=0, row=0, ipadx=100)
Button(frm, text="Not yet").grid(column=0, row=3)
Button(frm, text="Quit", command=root.destroy).grid(column=2, row=3)

frm.pack(expand=True)
root.mainloop()

What I want to do with this failed code is to make to symmetrically position buttons on the inferior center and the label on the upper center, then, know if the user clicked to not yet button and store that in variable. I even made a cool drawing.

image
thanks

答案1

得分: 1

要在tkinter中实现您期望的布局和功能,您可以使用pack几何管理器来对小部件进行对称定位并处理按钮点击。以下是修改后的代码:

from tkinter import *
from tkinter import ttk

def on_not_yet_click():
    global user_clicked
    user_clicked = True

root = Tk()
root.title('aaaa?')
root.geometry('400x300')
root.config(bg='#F2B33D')

frm = Frame(root, bg='#F2B33D')
frm.pack(expand=True)

Label(frm, text="Hello World!").pack(pady=50)

Button(frm, text="Not yet", command=on_not_yet_click).pack(side=LEFT, padx=20)
Button(frm, text="Quit", command=root.destroy).pack(side=RIGHT, padx=20)

# 设置user_clicked的初始值为False
user_clicked = False

root.mainloop()

在这个修改后的代码中,我们使用pack方法来对标签和按钮进行对称定位。我们添加了一些填充以在标签和按钮之间创建间距。side参数设置为LEFT以用于“Not yet”按钮,设置为RIGHT以用于“Quit”按钮。

此外,我们定义了on_not_yet_click函数来处理“Not yet”按钮的点击事件。当按钮被点击时,它将user_clicked变量设置为True。

现在,如果用户点击“Not yet”按钮,user_clicked变量将为True,并且您可以根据需要在您的代码中使用它。

英文:

To achieve your desired layout and functionality in tkinter, you can use the pack geometry manager to position widgets symmetrically and handle button clicks. Here's the modified code:

from tkinter import *
from tkinter import ttk

def on_not_yet_click():
    global user_clicked
    user_clicked = True

root = Tk()
root.title('aaaa?')
root.geometry('400x300')
root.config(bg='#F2B33D')

frm = Frame(root, bg='#F2B33D')
frm.pack(expand=True)

Label(frm, text="Hello World!").pack(pady=50)

Button(frm, text="Not yet", command=on_not_yet_click).pack(side=LEFT, padx=20)
Button(frm, text="Quit", command=root.destroy).pack(side=RIGHT, padx=20)

# Set the initial value of user_clicked to False
user_clicked = False

root.mainloop()

In this modified code, we use the pack method to position the label and buttons symmetrically. We add some padding to create spacing between the label and buttons. The side argument is set to LEFT for the "Not yet" button and RIGHT for the "Quit" button.

Additionally, we define the on_not_yet_click function to handle the click event of the "Not yet" button. When the button is clicked, it sets the user_clicked variable to True.

Now, if the user clicks the "Not yet" button, the user_clicked variable will be True, and you can use it in your code as needed.

huangapple
  • 本文由 发表于 2023年8月4日 05:32:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定