gpt4 book ai didi

Python/Tkinter : How to reduce the size of the expanded frames to their first positions?

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:40 25 4
gpt4 key购买 nike

根据以下代码;当应用程序首次运行时,屏幕上会显示两个按钮。如果用户单击其中一个按钮,框架就会展开并可以看到新的按钮。如果用户单击新按钮,则会展开另一个框架,并且可以再次看到新按钮。

例如,如果用户首先单击“English”按钮,则可以看到“Expand”按钮。如果用户单击“展开”按钮,则可以看到“数据”按钮。此后,如果用户单击“土耳其语”按钮,“展开”按钮将更改为“Genişlet”,但“数据” ”按钮仍然继续显示,最后如果用户点击“Genişlet”按钮,“Data”按钮将更改为“Veri”.

但是上面的操作不是我想要做的。我想通过点击“英语”“土耳其语”来更改“Veri”“数据”按钮> 按钮。

那么,为了做到这一点,我应该修改代码的哪些部分?预先感谢您。

import tkinter as tk


class App(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid(row=0, column=0, sticky="nsew")

self.b1 = tk.Button(master=self, text="Turkish", width=20)
self.b1.grid(row=0, column=0)
self.b2 = tk.Button(master=self, text="English", width=20)
self.b2.grid(row=0, column=1)

self.f1 = tk.Frame(master=master)
self.f1.grid(row=1, column=0)

self.f2 = tk.Frame(master=master)
self.f2.grid(row=2, column=0)

self.f3 = tk.Frame(master=self.f1)
self.f4 = tk.Frame(master=self.f1)

self.b3 = tk.Button(master=self.f3, text="Genişlet")
self.b3.grid(row=0, column=0)
self.b4 = tk.Button(master=self.f4, text="Expand")
self.b4.grid(row=0, column=0)

self.f5 = tk.Frame(master=self.f2)
self.f6 = tk.Frame(master=self.f2)

self.b5 = tk.Button(master=self.f5, text="Veri")
self.b5.grid(row=0, column=0)
self.b6 = tk.Button(master=self.f6, text="Data")
self.b6.grid(row=0, column=0)

self.configure_buttons()

@staticmethod
def activate(frame, parent):
for child in parent:
child.grid_forget()
frame.grid(row=0, column=0)

def configure_buttons(self):
self.b1.configure(command=lambda: self.activate(self.f3, self.f1.winfo_children()))
self.b2.configure(command=lambda: self.activate(self.f4, self.f1.winfo_children()))
self.b3.configure(command=lambda: self.activate(self.f5, self.f2.winfo_children()))
self.b4.configure(command=lambda: self.activate(self.f6, self.f2.winfo_children()))


if __name__ == "__main__":
root = tk.Tk()
frame = App(master=root)
frame.mainloop()

最佳答案

以下示例保留了您当前拥有的功能,同时能够使用 textvariablestringVar() 应用语言更改

我确信有更好的方法,但对于这个简单的程序来说,这应该足够了。

我创建了两个设置为 StringVar() 的变量。前 2 个按钮链接到一个函数/方法,该函数/方法将更改每个 stringVar 的字符串以反射(reflect)语言选择。

我还创建了一些占位符变量以供使用,直到需要创建其他按钮为止。让我知道您对此选项的看法。

更新:我添加了一个菜单,该菜单将删除除开始的 2 个按钮之外的所有按钮。实际上是重新启动。

import tkinter as tk


class App(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)

self.master = master
self.btn1_text = tk.StringVar()
self.btn1_text.set("Expand")
self.btn2_text = tk.StringVar()
self.btn2_text.set("Data")
self.second_frame = "None"
self.btn2 = "None"
self.master.columnconfigure(0, weight = 1)

self.top_frame = tk.Frame(self.master)
self.top_frame.grid(row = 0, column = 0, sticky = "ew")

self.turkish_button = tk.Button(self.top_frame, text="Turkish", width=20, command = lambda: self.change_lang_and_add_btn1("turkish"))
self.turkish_button.grid(row=0, column=0)
self.english_button = tk.Button(self.top_frame, text="English", width=20, command = lambda: self.change_lang_and_add_btn1("english"))
self.english_button.grid(row=0, column=1)

self.menu = tk.Menu(self.master)
self.master.config(menu = self.menu)
self.file_menu = tk.Menu(self.menu, tearoff = 0)
self.menu.add_cascade(label = "File", menu = self.file_menu)
self.file_menu.add_command(label = "Reset", command = self.reset_buttons)

def change_lang_and_add_btn1(self, choice):
if choice == "english":
self.btn1_text.set("Expand")
self.btn2_text.set("Data")
if choice == "turkish":
self.btn1_text.set("Genişlet")
self.btn2_text.set("Veri")

if self.second_frame == "None":
self.second_frame = tk.Frame(self.master)
self.second_frame.grid(row = 1, column = 0, columnspan = 2)
self.btn1 = tk.Button(self.second_frame, textvariable = self.btn1_text, width=20, command = lambda: self.add_btn2())
self.btn1.grid(row = 1, column = 0, columnspan = 2)

def add_btn2(self):
if self.btn2 == "None":
self.btn2 = tk.Button(self.second_frame, textvariable = self.btn2_text, width=20)
self.btn2.grid(row = 2, column = 0, columnspan = 2)

def reset_buttons(self):
if self.second_frame != "None":
self.second_frame.destroy()
self.second_frame = "None"
self.btn2 = "None"


if __name__ == "__main__":
root = tk.Tk()
frame = App(root)
frame.mainloop()

关于Python/Tkinter : How to reduce the size of the expanded frames to their first positions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com