我正在编写一个程序来保持14-1台球(池)比赛的得分。由于我需要多个窗口,所以我使用了tkinter帧。 2页的框架可以解决困难。我的问题是输入数据。每个玩家姓名中输入的第一个项目。两个文本框都出现在屏幕上,并且可以输入数据。如果我在输入后尝试显示数据,我什么也得不到。我已经做了好几天了。而且我确定有人可以看一下,然后说嘿嘿笨拙的头,你确实这样做了。适当的一些帮助。
import tkinter as tk
from tkinter import *
TITLE_FONT = ("Helvetica", 18, "bold")
TITLE_FONT_LINE2 = ("Helvetica", 14, "bold")
class SampleApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
'''Show a frame for the given page name'''
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
player1 = StringVar()
player2 = StringVar()
game_balls = IntVar()
tk.Frame.__init__(self, parent)
self.controller = controller
self.configure(background='green')
self.controller.title('Billard Score Keeping')
label1 = tk.Label(self,text="This Program is for Scoring 14-1 Straight Pool", font=TITLE_FONT, bg = 'green')
label1.pack()
label = tk.Label(self, text="Championship 14-1 Match is 150 Balls", font=TITLE_FONT_LINE2, bg = 'green')
label.pack()
label2 = tk.Label(self,text="Player One Name ", font=TITLE_FONT_LINE2, bg = 'green').place(x=45,y=70)
label3 = tk.Label(self,text='Player Two Name', font=TITLE_FONT_LINE2, bg = 'green').place(x=445,y=70)
label4 = tk.Label(self,text='Enter Number of Balls in Match', font=TITLE_FONT_LINE2, bg = 'green').place(x=185,y=110)
# Here is where I'm Having difficulty:
self.entry = tk.Entry(self, text=player1, width=7, font=TITLE_FONT_LINE2).place(x=60,y=100)
self.entry = tk.Entry(self,text=player2, width=7, font=TITLE_FONT_LINE2).place(x=480,y=100)
self.entry = tk.Entry(self,text=game_balls, width=4, font=TITLE_FONT_LINE2).place(x=275,y=140)
button1 = tk.Button(self, text="Go to Page One",
command=lambda: controller.show_frame("PageOne"))
button2 = tk.Button(self, text="Go to Page Two",
command=lambda: controller.show_frame("PageTwo"))
button1.place(x=275,y=440)
button2.place(x=275,y=540)
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.controller.geometry('615x615+415+190')
label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button = tk.Button(self, text="Go to the start page",
command=lambda: controller.show_frame("StartPage"))
button.pack()
if __name__ == "__main__":
app = SampleApp()
app.mainloop()
答案 0 :(得分:0)
我看不到任何尝试访问文本条目窗口小部件的内容。因此,您的代码完全符合我的期望。
如果您想阅读文本条目小部件的内容,您应该创建一个StringVar
来保存内容
self.playerone = StringVar()
self.entry1 = ttk.Entry(parent, textvariable=playerone)
然后可以从playerone
变量的内容中读取条目的内容,例如playeronename = self.playerone.get()
(当然变量必须使用带有命名空间的完全限定名称)
http://www.tkdocs.com/tutorial/widgets.html#entry和http://effbot.org/tkinterbook/entry.htm
上的条目小部件有文档答案 1 :(得分:-1)
您的代码和帖子太乱了。 关于你的问题:我建议你使用GET方法,从每个输入框中使用GET方法获取数据,然后将其归为变量,这是从输入框中获取数据的最简单方法。在像你这样的应用程序中,我在输入框附近创建按钮,在命令属性中我做了类似这样的事情
bttn = Button(self, text='enter', command=self.txt_box.get(0, END))