Python - tkinter - 在某些 window 中放置不同的网格?
Python - tkinter - put different grids in some window?
################################################
# Left | Right #
#-----------------------|----------------------#
# hello| hello | | hello| hello | hello#
#-----------------------|------|--------|------#
# | hello| #
################################################
我想生成一些上面那种类型的 tkinter window。 Left 和 Right 是两个按钮。如果我点击左侧,一个新的小部件 window 打开并带有一个应用按钮。如果我点击应用程序应该记住我从哪个按钮打开 Window 和应用按钮并在左边(如果先按下左按钮)或右边(如果右按钮是先按下)。
我现在的程序是这样的:
################################################
# Left | Right #
#-----------------------|----------------------#
# hello | hello #
#-----------------------|----------------------#
# | hello #
################################################
如何将样式从第二种解决方案更改为第一种?
我的解决方案二的代码:
from tkinter import *
class NewWindow(Toplevel):
def __init__(self, master = None, apply=None):
super().__init__(master = master)
self.title('NewWindow')
self.master = master
self.words = 'Hello'
self.bt1 = Button(self, text="apply", command=self.bt_press)
self.bt1.grid(column=0, row=0)
self.apply = apply
def bt_press(self):
self.apply(self.words)
self.destroy()
root = Tk()
def new_Editor(key):
def make_label1(lbl_txt):
print("root.grid_slaves(0):", root.grid_slaves(column=0))
row = len(root.grid_slaves(column=0))+1
lbl = Label(root, text=lbl_txt)
lbl.grid(row=row, column=0)
def make_label2(lbl_txt):
print("root.grid_slaves(1):", root.grid_slaves(column=1))
row = len(root.grid_slaves(column=1))+1
lbl = Label(root, text=lbl_txt)
lbl.grid(row=row, column=1)
if key == 1:
a = NewWindow(root, make_label1)
else:
a = NewWindow(root, make_label2)
root.title("BasicWindow")
root.basic_bt_l = Button(root, text="Left", command=lambda: new_Editor(1))
root.basic_bt_l.grid(column=0, row=0)
root.basic_bt_r = Button(root, text="Right", command=lambda: new_Editor(2))
root.basic_bt_r.grid(column=1, row=0)
root.mainloop()
我建议为标签创建两个框架,一个用于左,另一个用于右。
然后可以根据每一帧的label个数来确定要添加的新label的行和列。
from tkinter import *
class NewWindow(Toplevel):
def __init__(self, master = None, apply=None):
super().__init__(master = master)
self.title('NewWindow')
self.master = master
self.words = 'Hello'
self.bt1 = Button(self, text="apply", command=self.bt_press)
self.bt1.grid(column=0, row=0)
self.apply = apply
def bt_press(self):
self.apply(self.words)
self.destroy()
root = Tk()
# adjust the below two settings to suit your requirement
COLS = 3
COLWIDTH = 50
def new_Editor(key):
def make_label1(lbl_txt):
# get the children of left frame
slaves = root.left_frame.grid_slaves()
print("root.left_frame.grid_slaves():", slaves)
# determine the row and column of the new label
row, col = divmod(len(slaves), COLS)
lbl = Label(root.left_frame, text=lbl_txt)
lbl.grid(row=row, column=col)
def make_label2(lbl_txt):
# get the children of right frame
slaves = root.right_frame.grid_slaves()
print("root.right_frame.grid_slaves():", slaves)
# determine the row and column of the new label
row, col = divmod(len(slaves), COLS)
lbl = Label(root.right_frame, text=lbl_txt)
lbl.grid(row=row, column=col)
if key == 1:
a = NewWindow(root, make_label1)
else:
a = NewWindow(root, make_label2)
root.title("BasicWindow")
# make the two column same size
root.columnconfigure((0,1), weight=1, uniform=1, minsize=COLS*COLWIDTH)
root.basic_bt_l = Button(root, text="Left", command=lambda: new_Editor(1))
root.basic_bt_l.grid(column=0, row=0)
root.basic_bt_r = Button(root, text="Right", command=lambda: new_Editor(2))
root.basic_bt_r.grid(column=1, row=0)
# frame for left labels
root.left_frame = Frame(root)
root.left_frame.grid(row=1, column=0, sticky='nsew')
# make all the columns inside the frame same size
root.left_frame.columnconfigure(list(range(COLS)), weight=1, uniform=1, minsize=COLWIDTH)
# frame for right labels
root.right_frame = Frame(root)
root.right_frame.grid(row=1, column=1, sticky='nsew')
# make all the columns inside the frame same size
root.right_frame.columnconfigure(list(range(COLS)), weight=1, uniform=1, minsize=COLWIDTH)
root.mainloop()
结果:
################################################
# Left | Right #
#-----------------------|----------------------#
# hello| hello | | hello| hello | hello#
#-----------------------|------|--------|------#
# | hello| #
################################################
我想生成一些上面那种类型的 tkinter window。 Left 和 Right 是两个按钮。如果我点击左侧,一个新的小部件 window 打开并带有一个应用按钮。如果我点击应用程序应该记住我从哪个按钮打开 Window 和应用按钮并在左边(如果先按下左按钮)或右边(如果右按钮是先按下)。
我现在的程序是这样的:
################################################
# Left | Right #
#-----------------------|----------------------#
# hello | hello #
#-----------------------|----------------------#
# | hello #
################################################
如何将样式从第二种解决方案更改为第一种?
我的解决方案二的代码:
from tkinter import *
class NewWindow(Toplevel):
def __init__(self, master = None, apply=None):
super().__init__(master = master)
self.title('NewWindow')
self.master = master
self.words = 'Hello'
self.bt1 = Button(self, text="apply", command=self.bt_press)
self.bt1.grid(column=0, row=0)
self.apply = apply
def bt_press(self):
self.apply(self.words)
self.destroy()
root = Tk()
def new_Editor(key):
def make_label1(lbl_txt):
print("root.grid_slaves(0):", root.grid_slaves(column=0))
row = len(root.grid_slaves(column=0))+1
lbl = Label(root, text=lbl_txt)
lbl.grid(row=row, column=0)
def make_label2(lbl_txt):
print("root.grid_slaves(1):", root.grid_slaves(column=1))
row = len(root.grid_slaves(column=1))+1
lbl = Label(root, text=lbl_txt)
lbl.grid(row=row, column=1)
if key == 1:
a = NewWindow(root, make_label1)
else:
a = NewWindow(root, make_label2)
root.title("BasicWindow")
root.basic_bt_l = Button(root, text="Left", command=lambda: new_Editor(1))
root.basic_bt_l.grid(column=0, row=0)
root.basic_bt_r = Button(root, text="Right", command=lambda: new_Editor(2))
root.basic_bt_r.grid(column=1, row=0)
root.mainloop()
我建议为标签创建两个框架,一个用于左,另一个用于右。
然后可以根据每一帧的label个数来确定要添加的新label的行和列。
from tkinter import *
class NewWindow(Toplevel):
def __init__(self, master = None, apply=None):
super().__init__(master = master)
self.title('NewWindow')
self.master = master
self.words = 'Hello'
self.bt1 = Button(self, text="apply", command=self.bt_press)
self.bt1.grid(column=0, row=0)
self.apply = apply
def bt_press(self):
self.apply(self.words)
self.destroy()
root = Tk()
# adjust the below two settings to suit your requirement
COLS = 3
COLWIDTH = 50
def new_Editor(key):
def make_label1(lbl_txt):
# get the children of left frame
slaves = root.left_frame.grid_slaves()
print("root.left_frame.grid_slaves():", slaves)
# determine the row and column of the new label
row, col = divmod(len(slaves), COLS)
lbl = Label(root.left_frame, text=lbl_txt)
lbl.grid(row=row, column=col)
def make_label2(lbl_txt):
# get the children of right frame
slaves = root.right_frame.grid_slaves()
print("root.right_frame.grid_slaves():", slaves)
# determine the row and column of the new label
row, col = divmod(len(slaves), COLS)
lbl = Label(root.right_frame, text=lbl_txt)
lbl.grid(row=row, column=col)
if key == 1:
a = NewWindow(root, make_label1)
else:
a = NewWindow(root, make_label2)
root.title("BasicWindow")
# make the two column same size
root.columnconfigure((0,1), weight=1, uniform=1, minsize=COLS*COLWIDTH)
root.basic_bt_l = Button(root, text="Left", command=lambda: new_Editor(1))
root.basic_bt_l.grid(column=0, row=0)
root.basic_bt_r = Button(root, text="Right", command=lambda: new_Editor(2))
root.basic_bt_r.grid(column=1, row=0)
# frame for left labels
root.left_frame = Frame(root)
root.left_frame.grid(row=1, column=0, sticky='nsew')
# make all the columns inside the frame same size
root.left_frame.columnconfigure(list(range(COLS)), weight=1, uniform=1, minsize=COLWIDTH)
# frame for right labels
root.right_frame = Frame(root)
root.right_frame.grid(row=1, column=1, sticky='nsew')
# make all the columns inside the frame same size
root.right_frame.columnconfigure(list(range(COLS)), weight=1, uniform=1, minsize=COLWIDTH)
root.mainloop()
结果: