Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python程序卡在窗口中_Python_Tkinter_Pycharm - Fatal编程技术网

python程序卡在窗口中

python程序卡在窗口中,python,tkinter,pycharm,Python,Tkinter,Pycharm,我有一个用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_

我有一个用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()
从Tkinter导入*
导入tkFileDialog
从PIL导入图像,ImageTk
类应用程序:
label_rgb_template=“R:{0},G:{1},B:{2}”
定义初始化(自):
self.picked_image_filename=无
self.image=None
self.tk_image=None
self.image\u id=None
self.image\u文件名=无
self.current\u size=无
自电流系数=无
self.root=root=Tk()
self.offset_x=0
self.offset_y=0
root.title('生物材料')
root.size=(500500)
root.grid\u columnconfigure(0,权重=1)
root.grid_rowconfigure(1,权重=1)
root.bind(“,self.resize\u处理程序)
root.bind(“,self.root\u key\u press\u处理程序)
帧=帧(根)
frame.grid(列=0,行=0,粘性=N+S+W+E)
frame.grid\u column配置(10,权重=1)
btn_open=按钮(框架,text='otworz plik',command=self.open_图像)
btn_save=按钮(frame,text='zapisz plik',command=self.save_image)
btn_zoom_in=按钮(框架,文本=“+”,命令=self.zoom_in)
btn\u zoom\u out=按钮(框架,文本='-',命令=self.zoom\u out)
self.entry\u red=entry\u red=Spinbox(帧,从0到255)
标签\输入\红色=标签(框,text=“R:)
self.entry\u green=entry\u green=Spinbox(帧,从0到255)
label\u entry\u green=标签(frame,text=“G:”)
self.entry\u blue=entry\u blue=Spinbox(帧,从0到255)
label\u entry\u blue=标签(框,text=“B:)
self.label\u rgb\u text=StringVar()
self.label\u rgb\u text.set(self.label\u rgb\u template.format(0,0,0))
label\u rgb=label(框架,textvariable=self.label\u rgb\u文本)
btn_开放网格(行=0,列=0)
btn_save.grid(行=0,列=1)
btn\u放大网格(行=0,列=2)
btn\u缩小.grid(行=0,列=3)
标签\条目\红色网格(行=0,列=4)
输入红色网格(行=0,列=5)
标签\条目\绿色.grid(行=0,列=6)
条目\绿色网格(行=0,列=7)
label\u entry\u blue.grid(行=0,列=8)
条目\蓝色网格(行=0,列=9)
标签网格(行=0,列=10,粘性=E)
self.canvas=canvas=canvas(根)
网格(行=1,列=0,粘性=N+S+W+E)
canvas.bind(“,self.canvas\u click\u处理程序)
canvas.bind(“,self.canvas\u motion\u handler)
self.root.mainloop()
def打开_映像(自身):
self.image\u filename=image\u filename=tkFileDialog.askopenfilename(文件类型=('All files','*.'),)
self.image=image.open(image\u文件名)
self.image=self.image.convert(“RGB”)
self.current_size=self.image.size
自电流系数=1
self.refresh()
def保存图像(自身):
image\u filename=tkFileDialog.asksaveasfilename(initialfile=self.image\u filename)
self.image.save(图像\文件名)
def缩放(自身、系数):
self.current\u size=元组((int(值*因子)表示self.current\u size中的值))
self.current\u factor=int(self.current\u factor*factor)
self.refresh()
def放大(自):
自动缩放(2)
def缩小(自):
自动缩放(0.5)
def刷新(自我):
中心=(self.canvas.winfo_width()/2+self.offset_x,self.canvas.winfo_height()/2+self.offset_y)
self.canvas.delete(self.image\u id)
self.tk_image=ImageTk.PhotoImage(self.image.resize(self.current_size))
self.image\u id=self.canvas.create\u image(中心,image=self.tk\u image)
def resize_处理程序(自身、evt):
如果自我形象:
self.refresh()
def获取像素坐标(自身、evt):
bbox=self.canvas.bbox(全部)

如果bbox和bbox[0]如果这是您的全部代码,则您忘记了创建
应用程序的实例。将以下内容添加到文件底部:

if __name__ == "__main__":
    app = App()

如果这是您的全部代码,那么您忘记了创建
App
的实例。将以下内容添加到文件底部:

if __name__ == "__main__":
    app = App()