2017-12-16 9 views
1

github의 예제를 python2에서 python3으로 변환하고 이해가 안되는 문제로 변환하려고합니다.이 예제를 python2에서 python3으로 변환하는 방법

원래 내가 다음 패치를 적용하고 여기서 SampleApp 초기화 방법에 너무 많은 인수에 대한 오류 메시지가 https://gist.github.com/EugeneBakin/76c8f9bcec5b390e45df 입니다. 유일한 호출이 아무런 언급도하지 않았을 때 왜 원본이 그것의 args와 ** kwargs를 가지고 있었는지 나는 이해하지 못하기 때문에, 무언가는 나를 위해 합산하지 않는다. 그러나 그것은 python2에서 작동했기 때문에 python3에서도 사용하기를 바랬습니다. 사용법이 유용하다고 인식했기 때문입니다.

그러나 오류 메시지의 스택 추적에 해당 통화 또는 해당 통화에 대한 통화가 포함되어 있지 않기 때문에 문제가되지 않을 수 있습니다. 또 다른 퍼즐.

실제 사용 사례에 적용 할 때 필요할 수 있으므로 인수를 맹목적으로 제거하지 않고이 문제를 해결하고 싶습니다.

나는 또한 위험하다고 생각하고 독자에게 유용한 정보로 숨길 때 *로 가져 오기를 제거하려고합니다.

패치 :

--- VSFrame.py 2017-12-16 14:30:33.458923856 -0800 
+++ VSFrame3.py 2017-12-16 15:01:21.914423486 -0800 
@@ -1,21 +1,21 @@ 
-#!/usr/bin/python 
+#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
-from Tkinter import * # from x import * is bad practice 
-from ttk import * 
+import tkinter as tk 
+import tkinter.ttk as ttk 

# http://tkinter.unpythonic.net/wiki/VerticalScrolledFrame 

-class VerticalScrolledFrame(Frame): 
+class VerticalScrolledFrame(ttk.Frame): 
    """A pure Tkinter scrollable frame that actually works! 
    * Use the 'interior' attribute to place widgets inside the scrollable frame 
    * Construct and pack/place/grid normally 
    * This frame only allows vertical scrolling 
    """ 
    def __init__(self, parent, *args, **kw): 
-  Frame.__init__(self, parent, *args, **kw)    
+  super().__init__(self, parent, *args, **kw)    

     # create a canvas object and a vertical scrollbar for scrolling it 
-  vscrollbar = Scrollbar(self, orient=VERTICAL) 
+  vscrollbar = ttk.Scrollbar(self, orient=VERTICAL) 
     vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE) 
     canvas = Canvas(self, bd=0, highlightthickness=0, 
         yscrollcommand=vscrollbar.set) 
@@ -51,9 +51,9 @@ 

if __name__ == "__main__": 

- class SampleApp(Tk): 
+ class SampleApp(tk): 
     def __init__(self, *args, **kwargs): 
-   root = Tk.__init__(self, *args, **kwargs) 
+   root = tk.__init__(self, *args, **kwargs) 


      self.frame = VerticalScrolledFrame(root) 

그리고 윌렘에 의해 주석 후

(즉 주셔서 감사합니다), 내가 그것 스타일에 더 많은 약을 준수하여 작업 가지고 지금

--- VSFrame.py 2017-12-16 14:30:33.458923856 -0800 
+++ VSFrame3.py 2017-12-16 16:04:00.938380716 -0800 
@@ -1,21 +1,21 @@ 
-#!/usr/bin/python 
+#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
-from Tkinter import * # from x import * is bad practice 
-from ttk import * 
+import tkinter as tk 
+import tkinter.ttk as ttk 

# http://tkinter.unpythonic.net/wiki/VerticalScrolledFrame 

-class VerticalScrolledFrame(Frame): 
+class VerticalScrolledFrame(ttk.Frame): 
    """A pure Tkinter scrollable frame that actually works! 
    * Use the 'interior' attribute to place widgets inside the scrollable frame 
    * Construct and pack/place/grid normally 
    * This frame only allows vertical scrolling 
    """ 
    def __init__(self, parent, *args, **kw): 
-  Frame.__init__(self, parent, *args, **kw)    
+  super().__init__(self, parent, *args, **kw)    

     # create a canvas object and a vertical scrollbar for scrolling it 
-  vscrollbar = Scrollbar(self, orient=VERTICAL) 
+  vscrollbar = ttk.Scrollbar(self, orient=VERTICAL) 
     vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE) 
     canvas = Canvas(self, bd=0, highlightthickness=0, 
         yscrollcommand=vscrollbar.set) 
@@ -51,10 +51,9 @@ 

if __name__ == "__main__": 

- class SampleApp(Tk): 
+ class SampleApp(tk.Tk): 
     def __init__(self, *args, **kwargs): 
-   root = Tk.__init__(self, *args, **kwargs) 
- 
+   root = tk.TK.__init__(self, *args, **kwargs) 

      self.frame = VerticalScrolledFrame(root) 
      self.frame.pack() 
+1

그것은해야한다'+ 클래스 여기서 SampleApp (tk.Tk) :' –

+0

더 나를 조금을 얻는다 @Willem하지만 덕분에 그것을 위해. 이제 모듈 .__ init __()에 대한 첫 번째 인수는 SampleApp가 아니라 str이어야한다고 불평합니다. – 4dummies

+1

with Python3은 Python 2에서 Python 3로 많은 요소를 변환 할 수있는 프로그램 [2to3] (https://docs.python.org/3.6/library/2to3.html)이 설치되어 있습니다. – furas

답변

0

입니다 https://docs.python.org/3.5/library/tkinter.html#a-simple-hello-world-program

전체 소스가 이어집니다.

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
import tkinter as tk 
import tkinter.ttk as ttk 

# Python3 version of http://tkinter.unpythonic.net/wiki/VerticalScrolledFrame 

class VerticalScrolledFrame(ttk.Frame): 
    """A pure Tkinter scrollable frame that actually works! 
    * Use the 'interior' attribute to place widgets inside the scrollable frame 
    * Construct and pack/place/grid normally 
    * This frame only allows vertical scrolling 
    """ 
    def __init__(self, parent): 
     super().__init__(parent) 

     # create a canvas object and a vertical scrollbar for scrolling it 
     vscrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL) 
     vscrollbar.pack(fill=tk.Y, side=tk.RIGHT, expand=tk.FALSE) 
     canvas = tk.Canvas(self, bd=0, highlightthickness=0, 
         yscrollcommand=vscrollbar.set) 
     canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=tk.TRUE) 
     vscrollbar.config(command=canvas.yview) 

     # reset the view 
     canvas.xview_moveto(0) 
     canvas.yview_moveto(0) 

     # create a frame inside the canvas which will be scrolled with it 
     self.interior = interior = ttk.Frame(canvas) 
     interior_id = canvas.create_window(0, 0, window=interior, 
              anchor=tk.NW) 

     # track changes to the canvas and frame width and sync them, 
     # also updating the scrollbar 
     def _configure_interior(event): 
      # update the scrollbars to match the size of the inner frame 
      size = (interior.winfo_reqwidth(), interior.winfo_reqheight()) 
      canvas.config(scrollregion="0 0 %s %s" % size) 
      if interior.winfo_reqwidth() != canvas.winfo_width(): 
       # update the canvas's width to fit the inner frame 
       canvas.config(width=interior.winfo_reqwidth()) 
     interior.bind('<Configure>', _configure_interior) 

     def _configure_canvas(event): 
      if interior.winfo_reqwidth() != canvas.winfo_width(): 
       # update the inner frame's width to fill the canvas 
       canvas.itemconfigure(interior_id, width=canvas.winfo_width()) 
     canvas.bind('<Configure>', _configure_canvas) 


if __name__ == "__main__": 

    class SampleApp(ttk.Frame): 
     def __init__(self, master=None): 
      super().__init__(master) 
      self.create_widgets() 

     def create_widgets(self): 
      self.frame = VerticalScrolledFrame(root) 
      self.frame.pack() 
      self.label = ttk.Label(text="Shrink the window to activate the scrollbar.") 
      self.label.pack() 
      buttons = [] 
      for i in range(10): 
       buttons.append(ttk.Button(self.frame.interior, text="Button " + str(i))) 
       buttons[-1].pack() 

    root = tk.Tk() 
    app = SampleApp(master=root) 
    app.mainloop()