2017-12-16 11 views
1

githubの動作例をpython2からpython3に変換しようとしていますが、わからない問題が発生しています。この例をpython2からpython3に変換する方法

元は、私は以下のパッチを適用し、SampleApp のinit方法にあまりにも多くの引数に関するエラーメッセージが表示されますhttps://gist.github.com/EugeneBakin/76c8f9bcec5b390e45df です。唯一の呼び出しがまったく引数をもたないとき、なぜ元のものが引数と** 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、それに感謝します。これでmodule .__ init __()の最初の引数はSampleAppではなくstrでなければならないと不平を言っています。 – 4dummies

+1

と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() 
関連する問題