################################################
# Left | Right #
#-----------------------|----------------------#
# hello| hello | | hello| hello | hello#
#-----------------------|------|--------|------#
# | hello| #
################################################
我想生成一些上述型別的 tkinter 視窗。左和右是兩個按鈕。如果我單擊左側,則會打開一個新的小部件視窗,其中包含一個按鈕應用。如果我點擊應用,程式應該記住我從哪個按鈕打開了帶有應用按鈕的視窗,并在左側(如果先按下左按鈕)或右下(如果先按下右按鈕)下的某個網格中排序單詞 hello。
我現在的程式是這樣的:
################################################
# 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()
uj5u.com熱心網友回復:
我建議為標簽創建兩個框架,一個用于left,另一個用于right。
然后您可以使用每幀中的標簽數量來確定要添加的新標簽的行和列。
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()
結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425912.html
