我一直在看我的代码一段时间,这是tkinter的新手。我的代码的目的是在Canvas
小部件内显示文本,而不覆盖标签。但不确定如何执行此操作:
我的代码如下:
from tkinter import *
class Example(Frame):
def printLabel(self):
self.hello = []
self.hello.append('Hello')
self.hello.append('World!')
return(self.hello)
def updatePanel(self):
self.panelA.config(text="{}".format(self.printLabel()))
def __init__(self, root):
Frame.__init__(self, root)
self.buttonA()
self.viewingPanel()
def buttonA(self):
self.firstPage = Button(self, text="Print Text", bd=1, anchor=CENTER, height = 11, width = 13, command=lambda: self.updatePanel())
self.firstPage.place(x=0, y=0)
def viewingPanel(self):
self.panelA = Label(self, bg='white', width=65, height=13, padx=3, pady=3, anchor=NW, text="")
self.panelA.place(x=100, y=0)
self.cl= Canvas(self.panelA,bg='WHITE',width=165,height=113,relief=SUNKEN)
canvas_id = self.cl.create_text(15, 15, anchor="nw")
self.xb= Scrollbar(self.panelA,orient="horizontal", command=self.cl.xview)
self.xb.pack(side=BOTTOM,fill=X)
self.xb.config(command=self.cl.xview)
self.yb= Scrollbar(self.panelA,orient="vertical", command=self.cl.yview)
self.yb.pack(side=RIGHT,fill=Y)
self.yb.config(command=self.cl.yview)
self.cl.itemconfig(canvas_id,font=('Consolas',9), text="{}".format(self.printLabel()))
self.cl.configure(scrollregion = self.cl.bbox("all"))
self.cl.config(xscrollcommand=self.xb.set, yscrollcommand=self.yb.set)
self.cl.config(width=250,height=150)
self.cl.pack(side=LEFT,expand=True,fill=BOTH)
def main():
root = Tk()
root.title("Tk")
root.geometry('378x176')
app = Example(root)
app.pack(expand=True, fill=BOTH)
root.mainloop()
if __name__ == '__main__':
main()
Hello World!
应该显示在Canvas
中,但没有括号,但是主要的问题是,当我单击Button
时,它只是与画布重叠,并且将附加内容打印到Label
。
Label
应该在Canvas
内部。
答案 0 :(得分:0)
以下是解决“主要问题”和“括号问题”的方法。注释中建议使用字符串accounts = {'hello':'h1'}
print(accounts)
started = ""
while started != "q":
started = input("Are you a registered user y/n: (if you want to quit press any key)")
if started == "n":
createLogin = input("Create your desired login name: ")
if createLogin in accounts:
print ("Login name already exist!")
else:
createPassw = input("Create your desired password password: ")
accounts[createLogin] = createPassw
print("User created!")
addemail = input("What is your email address :")
accounts[createLogin].append(addemail)
elif started == "y":
login = input("Enter your login name: ")
if login in accounts:
passw = input("Enter your password")
else:
print("user doesnt exist.")
if login in accounts and accounts[login] == passw:
print("Login successful!")
changepassword = input("Would you like to change your password?")
if changepassword in ["Y", "y"]:
newpassw = input("What do you want your new password to be?")
accounts[login] = newpassw
print("password succesfully changed!")
else:
print("Password was not changed.")
else:
print("Incorrect password!")
方法来解决后者。
join()
方法已被修改,因此它首先创建一个updatePanel()
小部件,其中包含您要显示的文本,然后是一个Label
“窗口”对象,将该小部件指定为其内容。您尝试执行此操作的方法的代码也已从其他类方法中删除。
Canvas