【发布时间】:2020-03-25 11:51:58
【问题描述】:
我一直在尝试制作一个健身类的练习程序,而且我刚刚开始学习 Tkinter 和 Python。我目前正在努力按照我的意愿放置框架和小部件,并使程序在单个窗口中工作(不创建新的 Tk() 对象)。代码如下:
from tkinter import *
SelectionSwitch = False
root = Tk()
root.minsize(width=800, height=600)
root.maxsize(width=800, height=600)
xrcslist = ['Pushup', 'Pullup', 'Boxjump', 'Squat', 'Wristcurl', 'Bicepcurl', 'Crunch', 'Lunge', 'Plank']
xrcslist.sort()
BasicFont = 'Calibri Bold'
TextStart = Label(root, height=3, text='Welcome', bg='#F2D7D2', fg='black', borderwidth=4, relief='groove', width=100)
ButtonStart = Button(root, width=25, height=4, text='Exercises', bg='#C70039', command=lambda: pressed())
frameSelection = Frame(root, bg='#DFF5DC', width=800, height=600) # frame on which all of the contents of Second Screen are placed
frameSpace1 = Frame(frameSelection, height=30, width=5, bg='blue') # intentionally made in blue color to see it(this is an empty frame simply to make a gap between the Label and Listbox)
frameSpace2 = Frame(frameSelection, height=280, width=5, bg='yellow') #intentionally made in yellow color to see it, but it seems like it's hidden behind the Listbox(this frame is supposed to separate two Listboxes, but it's not behaving as expected so I didn't add the second one yet)
frameSpace3 = Frame(frameSelection, height=30, width=5, bg='red') # intentionally made in red color to see it(another empty frame to separate listboxes from the button in the lower left corner)
ButtonBackSelection = Button(frameSelection, text='Back', width=15, height=4, command=lambda: main.YetAgain())
frameSelectionLists = Frame(frameSelection)
TextSelection = Label(frameSelection, width=100, height=3, text='Select exercises', bg='#F2D7D2', borderwidth=4, relief='groove')
lstbx1 = Listbox(frameSelection, width=17, height=8)
for i in xrcslist:
lstbx1.insert(END, i)
scroll = Scrollbar(root, command=lstbx1.yview) # binding the scrollbar to the listbox
#configs
TextSelection.config(font=(BasicFont,11))
TextStart.config(font=(BasicFont, 11))
ButtonBackSelection.config(font=(BasicFont, 11))
ButtonStart.config(font=(BasicFont, 11))
lstbx1.config(yscrollcommand=scroll.set)
class main(Frame):
def __init__(self):
MainScreen()
def YetAgain():
global SelectionSwitch
if SelectionSwitch == True: # this 'if' section checks if a window other than the first one is currently displayed
frameSelection.grid_forget()
SelectionSwitch = False
MainScreen()
else:
MainScreen()
class Exercise(object):
def __init__(self, type, musclegroup, calpersec, equipment):
self.type = type
self.musclegroup = musclegroup
self.calpersec = calpersec
self.equipment = equipment
def pressed(): # replaces the current window with the selection window when ButtonStart is pressed
global SelectionSwitch
SelectionSwitch = True
TextStart.grid_forget()
ButtonStart.grid_forget()
frameSelection.grid()
TextSelection.grid()
frameSpace1.grid()
lstbx1.grid(row=2,column=0)
scroll.grid()
scroll.place(in_=lstbx1, relx=1.0, relheight=1.0, bordermode="outside")
frameSpace2.grid(row=2, column=1)
frameSpace3.grid(row=3)
ButtonBackSelection.grid(row=4)
ButtonBackSelection.place(relx=0, rely=1, anchor=SW)
def MainScreen(): # window that is showed on launch
TextStart.grid(row=0)
ButtonStart.grid(row=1)
ButtonStart.place(relx=0.5, rely=0.5, anchor=CENTER)
root.mainloop()
#list of exercises
Pushup = Exercise('hypertrophy', ['chest', 'triceps'], None, None)
Pullup = Exercise('hypertrophy', ['upper back','biceps'], None, 'pullup bar')
Boxjump = Exercise('hypertrophy', ['quads', 'glutes', 'hamstrings'], 0.16, 'box or an elevation')
Squat = Exercise('hypertrophy', ['quads', 'glutes'], 0.15, None)
Wristcurl = Exercise('hypertrophy', ['forearms'], None, 'dumbbell or barbell')
Bicepcurl = Exercise('hypertrophy', ['biceps','brachialis','forearms'], None, 'dumbbell or barbell')
Crunch = Exercise('hypertrophy', ['abdominals', 'obliques'], 0.09, None)
Lunge = Exercise('hypertrophy',['quadriceps', 'glutes', 'hamstrings'], 0.1, None)
Plank = Exercise('hypertrophy', ['abdominals', 'lower back'], 0.05, None)
main()
程序启动时会发生以下情况:
这部分工作得很好,我打算稍后再添加一些东西。
但是,当按下 ButtonStart 时,会发生一些奇怪的事情:
我有几个问题:
- 为什么 frameSelection 不是 root 的大小,即使它的尺寸是 800x600,与 root 相同?
- 为什么小部件在第二个屏幕上完全搞砸了,即使我已经在需要的地方分配了行和列(对于 TextSelection 和 frameSpace1 没关系,因为我想无论如何我希望它们排在第一和第二)?
- 将启动时显示的小部件放在框架上而不是直接放在根上会更好吗?我试过这样做,但定位很乱,对
row、column、rowconfigure和columnconfigure没有响应,就像第二个屏幕一样。
【问题讨论】:
标签: python-3.x tkinter