使用tkinter将小部件紧密放置在一起时出现问题。

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

Problem with positioning widgets right next to each other with tkinter

问题

我有这段代码:

import tkinter as tk

root = tk.Tk()
root.geometry("400x300")
root.columnconfigure(0, weight=1, minsize=75)
root.rowconfigure(0, weight=1, minsize=50)

subjects = ["None"]

frm_new_grade = tk.Frame()
lbl_new_grade = tk.Label(text="New Grade", font="System")
lbl_add_subject = tk.Label(frm_new_grade, text="Subject:", font="System")
lbl_add_subject.grid(row=0, column=0, sticky="E")
subject_value = tk.StringVar(frm_new_grade)
subject_value.set("Select an Option")
question_menu = tk.OptionMenu(frm_new_grade, subject_value, *subjects)
question_menu_menu = root.nametowidget(question_menu.menuname)
question_menu_menu.config(font="System")
question_menu.config(font="System")
question_menu.grid(row=0, column=1, sticky="W")
btn_new_subject = tk.Button(frm_new_grade, text="+", font="System", width=2)
btn_new_subject.grid(row=0, column=2, padx=5, sticky="W")
lbl_add_grade = tk.Label(frm_new_grade, text="Grade:", font="System")
lbl_add_grade.grid(row=1, column=0, sticky="E")
ent_add_grade = tk.Entry(frm_new_grade, font="System", width=18)
ent_add_grade.grid(row=1, column=1, columnspan=2, sticky="W")
btn_add_grade = tk.Button(frm_new_grade, text="Add Grade", font="System")
btn_add_grade.grid(row=2, column=1, sticky="W", pady=5)

frm_new_grade.grid()

root.mainloop()

如果您的问题是希望btn_new_subject始终位于question_menu的旁边,可以尝试将btn_new_subjectcolumn属性更改为0,而不是2。这将使它位于同一列,因此它将始终与question_menu对齐。代码中已经有sticky属性设置为"W",这将确保它在列中左对齐。这将解决您的问题。

英文:

So, I have this piece of code:

import tkinter as tk

root = tk.Tk()
root.geometry("400x300")
root.columnconfigure(0, weight=1, minsize=75)
root.rowconfigure(0, weight=1, minsize=50)

subjects = ["None"]

frm_new_grade = tk.Frame()
lbl_new_grade = tk.Label(text="New Grade", font="System")
lbl_add_subject = tk.Label(frm_new_grade, text="Subject:", font="System")
lbl_add_subject.grid(row=0, column=0, sticky="E")
subject_value = tk.StringVar(frm_new_grade)
subject_value.set("Select an Option")
question_menu = tk.OptionMenu(frm_new_grade, subject_value, *subjects)
question_menu_menu = root.nametowidget(question_menu.menuname)
question_menu_menu.config(font="System")
question_menu.config(font="System")
question_menu.grid(row=0, column=1, sticky="W")
btn_new_subject = tk.Button(frm_new_grade, text="+", font="System", width=2)
btn_new_subject.grid(row=0, column=2, padx=5, sticky="W")
lbl_add_grade = tk.Label(frm_new_grade, text="Grade:", font="System")
lbl_add_grade.grid(row=1, column=0, sticky="E")
ent_add_grade = tk.Entry(frm_new_grade, font="System", width=18)
ent_add_grade.grid(row=1, column=1, columnspan=2, sticky="W")
btn_add_grade = tk.Button(frm_new_grade, text="Add Grade", font="System")
btn_add_grade.grid(row=2, column=1, sticky="W", pady=5)

frm_new_grade.grid()

root.mainloop()


My problem is that I want btn_new_subject to be right next to question_menu at all times, like when question_menu has "Select an Option", but not like when I select "None"

I tried to fix this using columnspan on ent_add_grade (like you see on my code), I thought that this with the sticky properties of btn_new_subject would make it so that they are right next to each other, but instead I get btn_new_subject placed like if it had no sticky property.

I'm quite new to programming and python so any feedback will be appreciated.

答案1

得分: 0

你可以在 question_menu.grid(...) 中设置 sticky="EW",这样 question_menu 将会水平填充单元格中的空间。

结果:

使用tkinter将小部件紧密放置在一起时出现问题。

使用tkinter将小部件紧密放置在一起时出现问题。

英文:

You can just set sticky="EW" in question_menu.grid(...) so that question_menu will fill the space horizontally in the cell.

Result:

使用tkinter将小部件紧密放置在一起时出现问题。

使用tkinter将小部件紧密放置在一起时出现问题。

答案2

得分: 0

  • lbl_new_grade.grid()grid()方法缺少括号中的参数。
  • 在第12行,为lbl_new_grade(frm_new_grade,添加root
  • 设置所有的row=xcolumn=0。其中x代表1、2和3。而row=2 column=1。以及btn_add_grade.grid(row=3, column=1
  • 如果您喜欢布局,请使用root.geometry("235x150")
  • 如果您希望布局无故障,请将索引设置为1,root.columnconfigure(1,
英文:
  • There is typo error missing for lbl_new_grade.grid()
  • In line 12, add root for lbl_new_grade(frm_new_grade,
  • Set all row=x, column=0. The x denote 1, 2 and 3. While row=2
    column=1. And for btn_add_grade.grid(row=3, column=1
  • If you like layout for root.geometry("235x150")
  • If you like w/out glitch for layout. Then set index to 1
    root.columnconfigure(1,

It is up to you to decide.

Snippet for readability:

import tkinter as tk

root = tk.Tk()
root.geometry("235x150")
root.columnconfigure(0, weight=1, minsize=75)
root.rowconfigure(3, weight=1, minsize=50)

subjects = ["None"]

frm_new_grade = tk.Frame()

lbl_new_grade = tk.Label(frm_new_grade,  text="New Grade", bg='red', font="System")
lbl_new_grade.grid(row=0, column=1)

lbl_add_subject = tk.Label(frm_new_grade, text="Subject:", font="System")
lbl_add_subject.grid(row=1, column=0, sticky="WE", pady=10)

subject_value = tk.StringVar(frm_new_grade)
subject_value.set("Select an Option")

question_menu = tk.OptionMenu(frm_new_grade, subject_value, *subjects)
question_menu_menu = root.nametowidget(question_menu.menuname)
question_menu_menu.config(font="System")
question_menu.config(font="System")
question_menu.grid(row=1, column=1, sticky="WE")

btn_new_subject = tk.Button(frm_new_grade, text="+", font="System", width=2)
btn_new_subject.grid(row=1, column=2, padx=5, sticky="WE")

lbl_add_grade = tk.Label(frm_new_grade, text="Grade:", font="System")
lbl_add_grade.grid(row=2, column=0, sticky="W")

ent_add_grade = tk.Entry(frm_new_grade, font="System", width=18)
ent_add_grade.grid(row=2, column=1, columnspan=2, sticky="W")

btn_add_grade = tk.Button(frm_new_grade, text="Add Grade", font="System")
btn_add_grade.grid(row=3, column=1, sticky="W", pady=5)

frm_new_grade.grid()

root.mainloop()

Screenshot:

使用tkinter将小部件紧密放置在一起时出现问题。

huangapple
  • 本文由 发表于 2023年2月18日 00:33:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486929.html
匿名

发表评论

匿名网友

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

确定