canvas 的两页链接在 python 中
two pages of canvas linked in python
在我的 GUI 中我使用了 canvas 我希望我的程序链接到两页第一页来编写 'welcome' 然后按下一个按钮将它移动到程序页面..
我尝试添加 Next 功能,但我迷路了
from tkinter import ttk
root = Tk()
root.geometry('400x400')
root.columnconfigure(0, weight=1)
MyImage = PhotoImage(file='222.png')
MyImage1 = PhotoImage(file='66.png')
#the welcome page
def Next():
global myCanvas, myCanvas2
myCanvas2.delete(all)
myCanvas = Canvas(root, width=350, height=300, bd=0, highlightthickness=0).pack(fill='both', expand=True)
myCanvas.create_image(0,0,image= MyImage, anchor='nw')
#Button for location
SaveLoc = Button(root, width=15, bg='#EDB491',fg='white', text='Choose location', command = openLocation)
myCanvas.create_window(145,154, anchor='nw', window=SaveLoc)
#Error message of locatino
LocError = myCanvas.create_text(200,187, text='You must select a location',fill='#E1B5C6', font=('jost', 10))
#File location
Folder_Name =""
def openLocation():
global Folder_Name
Folder_Name = filedialog.askdirectory()
if len(Folder_Name) > 1:
myCanvas.itemconfig(LocError,text=Folder_Name, fill='green')
else:
myCanvas.itemconfig(LocError,text='Please Choose Folder!!',fill='red')
myCanvas2 = Canvas(root, width=350, height=300, bd=0, highlightthickness=0)
myCanvas2.pack(fill='both', expand=True)
CanvasImage=myCanvas2.create_image(0,0,image= MyImage1, anchor='nw')
#Download button
Down = Button(root, width=10, bg='#EDB491',fg='white', text='go', command= Next)
DownW=myCanvas2.create_window(162,300, anchor='nw', window=Down)
root.mainloop()
这个问题在 Whosebug 上出现了数百次。
你犯了最常见的错误
variable = Widget.pack()
它将 None
分配给 variable
因为 pack()
/grid()
/place()
returns None
你必须分两步完成
variable = Widget
variable.pack()
你有
myCanvas = Canvas(...).pack(...)
所以它将 None
分配给 myCanvas
然后 myCanvas.create_image
意味着
None.create_image(...)
然后出现错误
AttributeError: 'NoneType' object has no attribute 'create_image'
你需要
myCanvas = Canvas(...)
myCanvas.pack(...)
编辑:
我的版本只有一个 Canvas
所以我不需要另一个 pack()
我做了其他更改 - 即。我使用 anchor="center"
将按钮和文本放在中间。
顺便说一句:PEP 8 -- Style Guidefor Python Code
import os
import tkinter as tk # PEP8 `import *` is not preferred
from tkinter import ttk
from tkinter import filedialog
# --- functions --- (PEP8: lower_case_names) (PEP8: all functions before main part)
def next_image():
# no need `global my_canvas` because I don't try to assign `=` to `my_canvas`
global save_loc # PEP8: `global` in separed lines
global loc_error
my_canvas.delete('all')
my_canvas.create_image(0, 0, image=my_image_2, anchor='nw')
#Button for location
save_loc = tk.Button(root, width=15, bg='#EDB491', fg='white', text='Choose location', command=open_location)
my_canvas.create_window(200, 200-20, anchor='center', window=save_loc)
#Error message of locatino
loc_error = my_canvas.create_text(200, 200+20, anchor='center', text='You must select a location', fill='#E1B5C6', font=('jost', 10))
def open_location():
global folder_name
folder_name = filedialog.askdirectory()
if folder_name:
my_canvas.itemconfig(loc_error, text=folder_name, fill='green')
else:
my_canvas.itemconfig(loc_error, text='Please Choose Folder!!', fill='red')
# --- main --- (PEP8: lower_case_names)
# folder with running code - to create full path to images
APP_HOME = os.path.dirname(os.path.abspath(__file__))
print('APP_HOME:', APP_HOME)
# File location # (PEP8: one space after `#`)
folder_name = ""
root = tk.Tk()
root.geometry('400x400')
#root.columnconfigure(0, weight=1) # don't need it if I don't use `grid()`
my_image_1 = tk.PhotoImage(file=os.path.join(APP_HOME, 'lenna.png'))
my_image_2 = tk.PhotoImage(file=os.path.join(APP_HOME, 'lenna.png'))
my_canvas = tk.Canvas(root, width=350, height=300, bd=0, highlightthickness=0)
my_canvas.pack(fill='both', expand=True)
canvas_image = my_canvas.create_image(0, 0, image=my_image_1, anchor='nw')
# Download button
down_button = tk.Button(root, width=10, bg='#EDB491', fg='white', text='GO', command=next_image)
my_canvas.create_window(200, 200, anchor='center', window=down_button)
root.mainloop()
我使用来自维基百科的图片:Lenna
lenna.png
在我的 GUI 中我使用了 canvas 我希望我的程序链接到两页第一页来编写 'welcome' 然后按下一个按钮将它移动到程序页面.. 我尝试添加 Next 功能,但我迷路了
from tkinter import ttk
root = Tk()
root.geometry('400x400')
root.columnconfigure(0, weight=1)
MyImage = PhotoImage(file='222.png')
MyImage1 = PhotoImage(file='66.png')
#the welcome page
def Next():
global myCanvas, myCanvas2
myCanvas2.delete(all)
myCanvas = Canvas(root, width=350, height=300, bd=0, highlightthickness=0).pack(fill='both', expand=True)
myCanvas.create_image(0,0,image= MyImage, anchor='nw')
#Button for location
SaveLoc = Button(root, width=15, bg='#EDB491',fg='white', text='Choose location', command = openLocation)
myCanvas.create_window(145,154, anchor='nw', window=SaveLoc)
#Error message of locatino
LocError = myCanvas.create_text(200,187, text='You must select a location',fill='#E1B5C6', font=('jost', 10))
#File location
Folder_Name =""
def openLocation():
global Folder_Name
Folder_Name = filedialog.askdirectory()
if len(Folder_Name) > 1:
myCanvas.itemconfig(LocError,text=Folder_Name, fill='green')
else:
myCanvas.itemconfig(LocError,text='Please Choose Folder!!',fill='red')
myCanvas2 = Canvas(root, width=350, height=300, bd=0, highlightthickness=0)
myCanvas2.pack(fill='both', expand=True)
CanvasImage=myCanvas2.create_image(0,0,image= MyImage1, anchor='nw')
#Download button
Down = Button(root, width=10, bg='#EDB491',fg='white', text='go', command= Next)
DownW=myCanvas2.create_window(162,300, anchor='nw', window=Down)
root.mainloop()
这个问题在 Whosebug 上出现了数百次。
你犯了最常见的错误
variable = Widget.pack()
它将 None
分配给 variable
因为 pack()
/grid()
/place()
returns None
你必须分两步完成
variable = Widget
variable.pack()
你有
myCanvas = Canvas(...).pack(...)
所以它将 None
分配给 myCanvas
然后 myCanvas.create_image
意味着
None.create_image(...)
然后出现错误
AttributeError: 'NoneType' object has no attribute 'create_image'
你需要
myCanvas = Canvas(...)
myCanvas.pack(...)
编辑:
我的版本只有一个 Canvas
所以我不需要另一个 pack()
我做了其他更改 - 即。我使用 anchor="center"
将按钮和文本放在中间。
顺便说一句:PEP 8 -- Style Guidefor Python Code
import os
import tkinter as tk # PEP8 `import *` is not preferred
from tkinter import ttk
from tkinter import filedialog
# --- functions --- (PEP8: lower_case_names) (PEP8: all functions before main part)
def next_image():
# no need `global my_canvas` because I don't try to assign `=` to `my_canvas`
global save_loc # PEP8: `global` in separed lines
global loc_error
my_canvas.delete('all')
my_canvas.create_image(0, 0, image=my_image_2, anchor='nw')
#Button for location
save_loc = tk.Button(root, width=15, bg='#EDB491', fg='white', text='Choose location', command=open_location)
my_canvas.create_window(200, 200-20, anchor='center', window=save_loc)
#Error message of locatino
loc_error = my_canvas.create_text(200, 200+20, anchor='center', text='You must select a location', fill='#E1B5C6', font=('jost', 10))
def open_location():
global folder_name
folder_name = filedialog.askdirectory()
if folder_name:
my_canvas.itemconfig(loc_error, text=folder_name, fill='green')
else:
my_canvas.itemconfig(loc_error, text='Please Choose Folder!!', fill='red')
# --- main --- (PEP8: lower_case_names)
# folder with running code - to create full path to images
APP_HOME = os.path.dirname(os.path.abspath(__file__))
print('APP_HOME:', APP_HOME)
# File location # (PEP8: one space after `#`)
folder_name = ""
root = tk.Tk()
root.geometry('400x400')
#root.columnconfigure(0, weight=1) # don't need it if I don't use `grid()`
my_image_1 = tk.PhotoImage(file=os.path.join(APP_HOME, 'lenna.png'))
my_image_2 = tk.PhotoImage(file=os.path.join(APP_HOME, 'lenna.png'))
my_canvas = tk.Canvas(root, width=350, height=300, bd=0, highlightthickness=0)
my_canvas.pack(fill='both', expand=True)
canvas_image = my_canvas.create_image(0, 0, image=my_image_1, anchor='nw')
# Download button
down_button = tk.Button(root, width=10, bg='#EDB491', fg='white', text='GO', command=next_image)
my_canvas.create_window(200, 200, anchor='center', window=down_button)
root.mainloop()
我使用来自维基百科的图片:Lenna
lenna.png