我在python2.7中有一个脚本在Windows上的Pycharm IDE中编写,我可以在ubuntu中运行相同的程序吗?
from Tkinter import *
import tkFileDialog
from PIL import Image, ImageTk
class App:
label_rgb_template = "R: {0}, G: {1}, B: {2}"
def __init__(self):
self.picked_image_filename = None
self.image = None
self.tk_image = None
self.image_id = None
self.image_filename = None
self.current_size = None
self.current_factor = None
self.root = root = Tk()
self.offset_x = 0
self.offset_y = 0
root.title('Biomteria')
root.size = (500, 500)
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=1)
root.bind("<Configure>", self.resize_handler)
root.bind("<Key>", self.root_key_press_handler)
frame = Frame(root)
frame.grid(column=0, row=0, sticky=N + S + W + E)
frame.grid_columnconfigure(10, weight=1)
btn_open = Button(frame, text='otworz plik', command=self.open_image)
btn_save = Button(frame, text='zapisz plik', command=self.save_image)
btn_zoom_in = Button(frame, text='+', command=self.zoom_in)
btn_zoom_out = Button(frame, text='-', command=self.zoom_out)
self.entry_red = entry_red = Spinbox(frame, from_=0, to=255)
label_entry_red = Label(frame, text="R:")
self.entry_green = entry_green = Spinbox(frame, from_=0, to=255)
label_entry_green = Label(frame, text="G:")
self.entry_blue = entry_blue = Spinbox(frame, from_=0, to=255)
label_entry_blue = Label(frame, text="B:")
self.label_rgb_text = StringVar()
self.label_rgb_text.set(self.label_rgb_template.format(0, 0, 0))
label_rgb = Label(frame, textvariable=self.label_rgb_text)
btn_open.grid(row=0, column=0)
btn_save.grid(row=0, column=1)
btn_zoom_in.grid(row=0, column=2)
btn_zoom_out.grid(row=0, column=3)
label_entry_red.grid(row=0, column=4)
entry_red.grid(row=0, column=5)
label_entry_green.grid(row=0, column=6)
entry_green.grid(row=0, column=7)
label_entry_blue.grid(row=0, column=8)
entry_blue.grid(row=0, column=9)
label_rgb.grid(row=0, column=10, sticky=E)
self.canvas = canvas = Canvas(root)
canvas.grid(row=1, column=0, sticky=N + S + W + E)
canvas.bind("<Button-1>", self.canvas_click_handler)
canvas.bind("<Motion>", self.canvas_motion_handler)
self.root.mainloop()
def open_image(self):
self.image_filename = image_filename = tkFileDialog.askopenfilename(filetypes=(('All files', '*.*'),))
self.image = Image.open(image_filename)
self.image = self.image.convert("RGB")
self.current_size = self.image.size
self.current_factor = 1
self.refresh()
def save_image(self):
image_filename = tkFileDialog.asksaveasfilename(initialfile=self.image_filename)
self.image.save(image_filename)
def zoom(self, factor):
self.current_size = tuple((int(value * factor) for value in self.current_size))
self.current_factor = int(self.current_factor * factor)
self.refresh()
def zoom_in(self):
self.zoom(2)
def zoom_out(self):
self.zoom(0.5)
def refresh(self):
center = (self.canvas.winfo_width() / 2 + self.offset_x, self.canvas.winfo_height() / 2 + self.offset_y)
self.canvas.delete(self.image_id)
self.tk_image = ImageTk.PhotoImage(self.image.resize(self.current_size))
self.image_id = self.canvas.create_image(center, image=self.tk_image)
def resize_handler(self, evt):
if self.image:
self.refresh()
def get_pixel_coords(self, evt):
bbox = self.canvas.bbox(ALL)
if bbox and bbox[0] <= evt.x <= bbox[2] and bbox[1] <= evt.y < bbox[3]:
pixel_coords = evt.x - bbox[0], evt.y - bbox[1]
return tuple((value / self.current_factor for value in pixel_coords))
return None
def canvas_click_handler(self, evt):
pixel_coords = self.get_pixel_coords(evt)
if pixel_coords:
color = self.entry_red.get(), self.entry_green.get(), self.entry_blue.get()
color = tuple(int(value) for value in color)
self.image.putpixel(pixel_coords, color)
self.refresh()
def canvas_motion_handler(self, evt):
pixel_coords = self.get_pixel_coords(evt)
if pixel_coords:
self.label_rgb_text.set(self.label_rgb_template.format(*self.image.getpixel(pixel_coords)))
def root_key_press_handler(self, evt):
code = evt.keycode
print code
if code == 37:
self.offset_x -= 10
if code == 38:
self.offset_y -=10
if code == 39:
self.offset_x += 10
if code == 40:
self.offset_y += 10
self.refresh()
我必须改变一些东西吗?因为当我在ubuntu上运行该脚本时,脚本窗口打开,但按钮不起作用
答案 0 :(得分:0)
如果这是您的所有代码,则您忘记创建App
的实例。将以下内容添加到文件的底部:
if __name__ == "__main__":
app = App()