TkinterでGUI作成。

makarohiraki2009-07-09


昨日のAmazonClient使って本を検索。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Tkinter as Tk
import tktable as ta
import amazon as am

""" AmazonBookSearchFrame"""
class AmazonBookSearchFrame(Tk.Frame):
    
    def __init__(self, master = None):
        Tk.Frame.__init__(self, master)
        self.master.wm_geometry("650x300+250+200")
        self.master.title(u"Amazon 本検索")

        #タイトルラベル
        titleLabel = Tk.Label(self, master, text = u"タイトル入力!!", font=('Helvetica', '10', 'bold'))
        titleLabel.grid(row = 0, column = 0, padx = 0, pady = 0)
        
        #タイトル入力
        self.titleStrVar = Tk.StringVar()
        self.titleInputEntry = Tk.Entry(self, textvariable = self.titleStrVar)
        self.titleInputEntry.grid(row = 1, column = 0, padx = 5, pady = 5)
        
        # 検索ボタン
        button = Tk.Button(self, text="検索", command=self.search, width = 10)
        button.grid(row = 1, column = 1, padx = 5, pady = 5)
        
        # テーブル
        self.tableVar = ta.ArrayVar(self)
        self.__set_table_title()
        table = ta.Table(self,
                             rows=10,
                             cols=3,
                             state='disabled',
                             width=10,
                             height=10,
                             titlerows=1,
                             colwidth=30,
                             variable=self.tableVar,
                             selecttype='row')
        table.grid(row = 2, column = 0, columnspan = 2,  sticky=Tk.W + Tk.E + Tk.N + Tk.S)

    
    def __set_table_title(self):
        # 列名設定
        titles = (u"タイトル", u"評価", u"値段")
        for i in range(len(titles)):
            index = "%d,%d" % (0, i)
            self.tableVar.set(index, titles[i])
        
    def search(self):
        # 検索開始
        param = {am.SearchWordType.TITLE: self.titleStrVar.get()}
        client = am.AmazonClient(param)
        response = client.doRequest()
        
        tableVar = self.tableVar
        if len(response) > 0:
            indexList = []
            for i in range(1, len(response)):
                indexList = ["%d,%d" % (i, x) for x in range(4)]
                print indexList
                tableVar.set(indexList[0], response[i].title)
                tableVar.set(indexList[1], response[i].averagerating)
                tableVar.set(indexList[2], response[i].price + u"円")
                
if __name__ == "__main__":
    main = AmazonBookSearchFrame()
    main.pack()
    main.mainloop()

tktableの使い方がよく分からないので調査中。
もっと色々と細かい設定がしたい。そもそもできるのか?