我一直在尝试编写适合健身的程序进行练习,而我才刚刚开始学习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()
以下是程序启动时发生的情况:Main Screen
这部分工作正常,我打算以后再添加一些内容。
但是,当按下ButtonStart时,发生了一些奇怪的事情:Second Screen
我有几个问题:
row
,column
,rowconfigure
和columnconfigure
都没有响应,就像第二个屏幕一样。编辑:修复了第二张图片的链接
我一直在尝试编写适合健身的程序进行练习,而我才刚刚开始学习Tkinter和Python。我目前正在努力的是根据需要放置框架和小部件...
主要问题是grid
和pack
命令旨在使包含的小部件在其子级周围增大或缩小。这称为几何传播