Q - ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: Q - ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse

Re: Q - ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse

โดย nuattawoot » 25/05/2018 3:40 pm

ลอง Print Packages ออกมาดูก่อนว่ามันมีจริงหรือปล่าว
Packages Selenium

Re: Q - ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse

โดย rangsan » 25/05/2018 12:07 pm

ผมได้ลองทำการเปลี่ยน Format ของ Path ใหม่แล้วครับแต่ก็ยังไม่ได้ครับ

ตัวอย่างที่ได้ลองทำ

โค้ด: เลือกทั้งหมด

C:\Documents and Settings\Administrator\Desktop\geckodriver.exe
ภาพตัวอย่าง
Selection_119.png
Selection_119.png (19.98 KiB) Viewed 1661 times
Error ที่พบ
Selection_120.png
Selection_120.png (50.52 KiB) Viewed 1661 times

Re: Q - ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse

โดย nuattawoot » 25/05/2018 11:48 am

พี่ว่า format Path ผิดและมั้ง
ถ้าเป็นของวินโดว์จะใช้แบบนี้ไม่ใช้หร่า
2561-05-25 11_40_22-.png
2561-05-25 11_40_22-.png (61.44 KiB) Viewed 1663 times
format windows

โค้ด: เลือกทั้งหมด

C:\Users\com010\Desktop\New folder
แบบที่ 1

โค้ด: เลือกทั้งหมด

'C:/Users'
แบบที่ 2 ทำงานได้ทั้งใน linux และ windows

โค้ด: เลือกทั้งหมด

'C:\\Users'
แบบที่ 3 if you have problems with some names you can also try raw strings:

โค้ด: เลือกทั้งหมด

r'C:\mydir'

Re: Q - ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse

โดย mindphp » 25/05/2018 11:43 am

ลองศึกษาจากตัวอย่างนี้

โค้ด: เลือกทั้งหมด

    self.cbutton.grid(row=10, column=3, sticky = W + E)
    self.bbutton= Button(root, text="Browse", command=self.browsecsv)
    self.bbutton.grid(row=1, column=3)

def browsecsv(self):
    from tkFileDialog import askopenfilename

    Tk().withdraw() 
    self.filename = askopenfilename()

def process_csv(self):
    if self.filename:
        with open(self.filename, 'rb') as csvfile:
            logreader = csv.reader(csvfile, delimiter=',', quotechar='|')
            rownum=0

            for row in logreader:    
                NumColumns = len(row)        
                rownum += 1

            Matrix = [[0 for x in xrange(NumColumns)] for x in xrange(rownum)] 

root = Tk()
window=Window(root)
root.mainloop()  

Q - ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse

โดย rangsan » 25/05/2018 11:32 am

ตอนนี้ต้องการให้ Python GUI( Tkinter ) เก็บ Path ที่ได้จากการ Browse เพื่อเก็บค่าของ Path นั้นส่งไปที่ไฟล์ที่ใช้ Run Selenium ครับ และตอนนี้ผมได้สร้างหน้า Forms ของการ Browse Path มาแล้วครับ แต่เมื่อ Browse Path มาแล้วต้องการให้ มันส่งค่าของ Path ไปที่ File ที่ใช่ในการ Run Selenium Webdriver ครับ

ตัวอย่างหน้า Forms
Form_GUI.png
Form_GUI.png (14.6 KiB) Viewed 1669 times
และเมื่อผมทำการเลือก Path ที่ใช้ในการเปิด Browser ของ Firefox จะเป็นตัว geckodriver ครับ
Path_GUI.png
Path_GUI.png (83.21 KiB) Viewed 1669 times
เมื่อกด Open ก็จะได้ Path มาแบบนี้ครับ
Ex_path.png
Ex_path.png (19.97 KiB) Viewed 1669 times
จากนั้นกด Run แล้วเกิด Error ครับ
Error.png
Error.png (53.1 KiB) Viewed 1669 times
จะเห็นว่าไฟล์ที่เกิด Error คือ selenium_autorat บรรทัดที่ 21 ซึ่งเป็นโค้ดที่ใช้เปิด Path ของ Browser Firefox ครับ คือตัว geckodriver

โค้ด: เลือกทั้งหมด

    def setUp(self):
        self.driver = webdriver.Firefox(executable_path=firefox)
ภาพตัวอย่างประกอบ
Selection_116.png
Selection_116.png (15.15 KiB) Viewed 1669 times
โดยตัวแปร firefox นั้นผมได้สั่งให้เก็บค่าจากไฟล์ที่ใช้ในการสร้าง Python GUI( Tkinter )

โค้ด: เลือกทั้งหมด

    def __init__(self,master):
        self.master = master
        # self.hostname = StringVar()
        self.pathwebdriver = StringVar()
        self.widgets()
            def login(self):
        # link = self.hostname.get()
        # __builtin__.base_url = link
        __builtin__.firefox = self.pathwebdriver.get()

        suite = unittest.TestLoader().loadTestsFromModule(selenium_autorat)
        unittest.TextTestRunner(verbosity=2).run(suite)

    def widgets(self):
        self.head = Label(self.master,text = 'SELECT PATH',font = ('',25),pady = 10)
        self.head.pack()
        self.logf = Frame(self.master,padx =10,pady = 10)
        Label(self.logf,text = 'Path Webdriver ',font = ('',20),pady=5,padx=5 ).grid(row=0)
        Entry(self.logf,textvariable = self.pathwebdriver,bd = 5,font = ('',15)).grid(row=0,column=1)
        Button(self.logf,text = ' RUN ',bd = 3 ,font = ('',12),padx=5,pady=5, command=self.login).grid(row=1,column=1)
        Button(self.logf,text = ' Browse ' ,bd = 3 ,font = ('',10),padx=5,pady=5, command=self.selection).grid(row=0,column=2)
        Button(self.logf,text = ' Close ',bd = 3 ,font = ('',12),padx=5,pady=5, command=root.destroy).grid(row=1,column=2)
        self.logf.pack()
ภาพตัวอย่างประกอบ
Selection_117.png
Selection_117.png (86.49 KiB) Viewed 1669 times
อยากทราบว่าเป็นเพราะอะไรครับ

ข้างบน