gpt4 book ai didi

python - TKinter 中的不同窗口

转载 作者:行者123 更新时间:2023-11-30 23:14:14 25 4
gpt4 key购买 nike

我无法弄清楚如何在 Tkinter 实例中运行多个应用程序。我计划有一个主窗口来调用其他应用程序来运行。

我还想让登录按钮设置用户名和密码字符串,然后关闭它自己的窗口。

我已经尝试过使用顶级窗口,但这限制了我格式化按钮的能力。例如,我不能使用框架...非常感谢任何有关我需要采取的方向的帮助。

我正在旋转我的轮子试图解决这个问题,我已经查看了许多不同的代码示例,并且我不断得到矛盾的方法来完成同一件事。我正在使用 python 3。

import tkinter as tk
import os
import json
from tkinter import Toplevel
cert = ""

username = ""
password = ""
base_url = ''
url = ''
splash_page = ''

class BuildApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()

def createWidgets(self):
self.lUSER = tk.Label(self, text="Username: ").pack(side="left")
self.eUSER = tk.Entry(self)
self.eUSER.pack(side="left")

self.lPASS = tk.Label(self, text="Password: ").pack(side="left")
self.ePASS = tk.Entry(self)
self.ePASS.pack(side="left")

#ive tried command= lambda: self.setCredentials(self.eUSER.get(),self.ePASS.get())
# command = self.setCredentials(self.eUSER.get(),self.ePASS.get())
# But none if it works....
self.LOGIN = tk.Button(self, text = "Login", fg="green" )
self.LOGIN.pack(side="left")

self.QUIT = tk.Button(self, text="QUIT", fg="red", command=self.destroy)
self.QUIT.pack(side="left")

self.mainloop()

def setCredentials(self,u,p):
username = u
password = p
print(username)
print(password)
self.destroy()

class SearchApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()
def createWidgets(self):
print("create somethng")

class MainApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()

def createWidgets(self):
#Build Data
self.bBuild = tk.Button(self, text="Build Data", command=self.build)
self.bBuild.pack(side="top")

#Search Data
self.bSearch = tk.Button(self, text="Search",command=self.search)
self.bSearch["text"] = "Search"
self.bSearch["command"] = self.search
self.bSearch.pack(side="top")

#quit
self.QUIT = tk.Button(self, text="QUIT", fg="red", command= root.destroy)
self.QUIT.pack(side="bottom")

def build(self):
print("Building")
root2 = tk.Tk()
buildApp = BuildApplication(master=root2)
buildApp.mainloop()

def search(self):
print("Search")
root3 = tk.Tk()
app2 = SearchApplication(master=root3)
app2.mainloop()

root = tk.Tk()
app = MainApplication(master=root)
app.master.title("fdsafds")

app.mainloop()

最佳答案

我修改了你的代码:

import tkinter as tk
import os
import json
from tkinter import Toplevel
cert = ""

username = ""
password = ""
base_url = ''
url = ''
splash_page = ''

class BuildApplication(tk.Frame):

def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()

def createWidgets(self):
self.lUSER = tk.Label(self, text="Username: ")
self.lUSER.pack(side="left")
self.eUSER = tk.Entry(self)
self.eUSER.pack(side="left")

self.lPASS = tk.Label(self, text="Password: ")
self.lPASS.pack(side="left")
self.ePASS = tk.Entry(self, show="*")
self.ePASS.pack(side="left")

#ive tried command= lambda: self.setCredentials(self.eUSER.get(),self.ePASS.get())
# command = self.setCredentials(self.eUSER.get(),self.ePASS.get())
# But none if it works....
self.LOGIN = tk.Button(self, text = "Login", fg="green", command=self.setCredentials )
self.LOGIN.pack(side="left")

self.QUIT = tk.Button(self, text="QUIT", fg="red", command=self.master.destroy)
self.QUIT.pack(side="left")

#self.mainloop()

def setCredentials(self):
username = self.eUSER.get()
password = self.ePASS.get()
print("username", username)
print("password", password)
self.master.destroy()

class SearchApplication(tk.Frame):

def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()

def createWidgets(self):
print()
self.some_abel = tk.Label(self, text="create somethng")
self.some_abel.pack(side="left")
self.quitb = tk.Button(self, text = "quit", fg="green", command=self.master.destroy )
self.quitb.pack(side="left")

class MainApplication(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.pack()
self.createWidgets()

def createWidgets(self):
#Build Data
self.bBuild = tk.Button(self, text="Build Data", command=self.build)
self.bBuild.pack(side="top")

#Search Data
self.bSearch = tk.Button(self, text="Search",command=self.search)
self.bSearch["text"] = "Search"
self.bSearch["command"] = self.search
self.bSearch.pack(side="top")

#quit
self.QUIT = tk.Button(self, text="QUIT", fg="red", command= self.master.destroy)
self.QUIT.pack(side="bottom")

def build(self):
print("Building")
root2 = tk.Toplevel()
buildApp = BuildApplication(master=root2)


def search(self):
print("Search")
root3 = tk.Toplevel()
app2 = SearchApplication(master=root3)

root = tk.Tk()
app = MainApplication(master=root)
app.master.title("fdsafds")

app.mainloop()

主要变化包括在 buildsearch 方法中使用 toplevel 窗口,而不是 Tk() 方法。一个程序中不应有多个 Tk().mainloops()。新窗口最好使用toplevel窗口。

我还更改了 setCredentials 方法,以便它实际工作并打印出用户名和密码,并在按登录时销毁其窗口。此外,我还向 SearchApplication 添加了一些小部件,以展示如何使其工作。

其他问题是这一行(以及与此类似的其他行):

self.lUSER = tk.Label(self, text="Username: ").pack(side="left")

它不会在这个特定的代码中造成直接麻烦,但是由于这一行,self.lUSERNone。原因是 pack (和 grid)返回 None。现在这可能不是问题,但以后它可以让您省去很多麻烦,因为您想知道为什么不能更改标签 self.lUSER 如果您愿意的话。

下面的 gif 展示了它的工作原理。希望对您有所帮助。

enter image description here

关于python - TKinter 中的不同窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28892492/

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