我正在尝试将标签放在tkinter框架上的另一个标签下,但我要么在另一个标签上打印一个标签,或者我似乎让我的框架在其下面翻了一倍。我该如何解决这个问题?
这是我正在使用的代码:
class teachingContent(Tk):
def __init__(self):
super(teachingContent, self).__init__()
self.nextLabel = None
self.nextLabelText = StringVar()
self.nextLabelText.set("Next window")
self.tutorTitleFont = font.Font(family='Helvetica', size=16, weight='bold')
self.tutorContentFont = font.Font(family='Helvetica', size=14, weight='normal')
self.tutorCodeFont = font.Font(family='Helvetica', size=12, weight='normal')
self.tutorButtonFont = font.Font(family = 'Helvetica', size=12, weight='bold')
self.title_height = 40
self.firstProgram_title_frame = ttk.Frame()
self.firstProgram_frame = ttk.Frame()
def nextButtonPressed(self):
pass
def prevButtonPressed(self):
pass
def formSize(self):
self.configure(bg = 'black') # Sets the colour of the gui screen to black
self.geometry("650x450+200+200") # Sets the size of the gui
self.title("Python Tutor")
self.nbutton = Button(text = "Next", command = self.nextButtonPressed, bg = 'grey25', activebackground = 'gray19',
activeforeground = 'DodgerBlue1', fg = 'royal blue', width = 8, font = self.tutorButtonFont).place(x=561,y=418)
self.pbutton = Button(text = "Previous", command = self.prevButtonPressed, bg = 'grey25', activebackground = 'gray19',
activeforeground = 'DodgerBlue1', fg = 'royal blue', width = 8, font = self.tutorButtonFont).place(x=0,y=418)
self.firstProgram_title_frame.configure(height=self.title_height)
self.firstProgram_title_frame['borderwidth'] = 2
self.firstProgram_title_frame.grid(column=0, 0, padx=33,
pady=50, sticky=(W, N, E))
self.firstProgram_frame.configure()
self.firstProgram_frame['borderwidth'] = 2
self.firstProgram_frame['relief'] = 'sunken'
self.firstProgram_frame.grid(column=0, row=0, padx=33, pady=50, sticky=(W, N, E))
def firstProgram(self):
self.formSize()
self.firstProgramTitleLabel = Label(self.firstProgram_title_frame, text = "A First Program", font=self.tutorTitleFont)
self.firstProgramTitleLabel.pack()
self.firstProgramLabel = Label(self.firstProgram_frame, text = "This program reads in two numbers, adds them together and then prints out the result",
font = self.tutorContentFont, wraplength = 500, width = 35, height = 14)
self.firstProgramLabel.grid(column=self.0, row=0, ipadx = 85, pady = 11, padx = 11, sticky=(N))
self.firstProgram = Label(self.firstProgram_frame, text = "# This program works out the result by adding 2 numbers\n# Rob Miles November 2012\n\n# Read the numbers\
\nfirstString = input ('Enter the first number: ')\nfirstNumber = int(firstString)\
\nsecondString = input ('Enter the second number: ')\nsecondNumber = int(secondString)\n\n# work out the sum\
\nresult = firstNumber + secondNumber\n\n# Display the result\nprint ('The result is: ', result)",
font = self.tutorCodeFont, wraplength = 500, width = 35, height = 14, justify = LEFT)
self.firstProgram.grid(column=self.0, row=0, ipadx = 85, pady = 11, padx = 11, sticky=(N))
tc = teachingContent()
tc.firstProgram()
答案 0 :(得分:1)
下次在这里投入一些工作代码,对吗?
我的追溯日志,让您的代码运行:
File "tkinterlabel.py", line 68
self.firstProgramLabel.grid(column=self.0, row=0, ipadx = 85, pady = 11, padx = 11, sticky=(N))
^
SyntaxError: invalid syntax
File "tkinterlabel.py", line 75
self.firstProgram.grid(column=self.0, row=0, ipadx = 85, pady = 11, padx = 11, sticky=(N))
^
SyntaxError: invalid syntax
File "tkinterlabel.py", line 50
self.firstProgram_title_frame.grid(column=0, 0, padx=33,
^
SyntaxError: non-keyword arg after keyword arg
Traceback (most recent call last):
File "tkinterlabel.py", line 3, in <module>
class teachingContent(Tk):
NameError: name 'Tk' is not defined
Traceback (most recent call last):
File "tkinterlabel.py", line 79, in <module>
tc = teachingContent()
File "tkinterlabel.py", line 11, in __init__
self.tutorTitleFont = font.Font(family='Helvetica', size=16, weight='bold')
NameError: name 'font' is not defined
Traceback (most recent call last):
File "tkinterlabel.py", line 80, in <module>
tc = teachingContent()
File "tkinterlabel.py", line 19, in __init__
self.firstProgram_title_frame = ttk.Frame()
NameError: name 'ttk' is not defined
你犯了错误:
一些更正:
self.firstProgram_title_frame.grid(column=0, row=0, padx=33, pady=5, sticky='n')
self.firstProgram_frame.grid(column=0, row=1, padx=33, sticky='n')
self.firstProgramTitleLabel.grid(column=0, row=0)
self.firstProgramLabel = Label(self.firstProgram_frame, text = "blabla", font = self.tutorContentFont, wraplength = 500, width = 35)
self.firstProgram.grid(column=0, row=1, sticky='n', ipadx = 85)
答案 1 :(得分:0)
您的问题相当广泛,而且您的代码无法运行,因此很难给出明确的答案。至少有三种方法可以将一个标签放在另一个标签上 - 包装,网格和位置。
我猜您关注self.firstProgramLabel
和self.firstProgram
。在这两种情况下,您都将它们放在同一行中,因此它们将在彼此之上,除非您将它们放在不同的列中。在一个,你尝试把它放在self.0
,我猜这是一个错字,你打算列0(零)。第二个没有指定列,因此默认为零。因此,两个小部件都在第0行第0列。如果您不希望它们彼此重叠,请更改行或更改列。
使用grid
时,最好始终明确指定行和列。