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

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

rangsan
PHP Hero Member
PHP Hero Member
โพสต์: 199
ลงทะเบียนเมื่อ: 30/04/2018 9:44 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย rangsan »

ตอนนี้ต้องการให้ 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 1664 times
และเมื่อผมทำการเลือก Path ที่ใช้ในการเปิด Browser ของ Firefox จะเป็นตัว geckodriver ครับ
Path_GUI.png
Path_GUI.png (83.21 KiB) Viewed 1664 times
เมื่อกด Open ก็จะได้ Path มาแบบนี้ครับ
Ex_path.png
Ex_path.png (19.97 KiB) Viewed 1664 times
จากนั้นกด Run แล้วเกิด Error ครับ
Error.png
Error.png (53.1 KiB) Viewed 1664 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 1664 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 1664 times
อยากทราบว่าเป็นเพราะอะไรครับ
It’s never too late to start again.
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

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

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

    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()  
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ภาพประจำตัวสมาชิก
nuattawoot
PHP VIP Members
PHP VIP Members
โพสต์: 2561
ลงทะเบียนเมื่อ: 05/06/2017 9:34 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย nuattawoot »

พี่ว่า format Path ผิดและมั้ง
ถ้าเป็นของวินโดว์จะใช้แบบนี้ไม่ใช้หร่า
2561-05-25 11_40_22-.png
2561-05-25 11_40_22-.png (61.44 KiB) Viewed 1658 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'
First Bug Love you
รูปภาพ
rangsan
PHP Hero Member
PHP Hero Member
โพสต์: 199
ลงทะเบียนเมื่อ: 30/04/2018 9:44 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย rangsan »

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

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

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

C:\Documents and Settings\Administrator\Desktop\geckodriver.exe
ภาพตัวอย่าง
Selection_119.png
Selection_119.png (19.98 KiB) Viewed 1656 times
Error ที่พบ
Selection_120.png
Selection_120.png (50.52 KiB) Viewed 1656 times
It’s never too late to start again.
ภาพประจำตัวสมาชิก
nuattawoot
PHP VIP Members
PHP VIP Members
โพสต์: 2561
ลงทะเบียนเมื่อ: 05/06/2017 9:34 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย nuattawoot »

ลอง Print Packages ออกมาดูก่อนว่ามันมีจริงหรือปล่าว
Packages Selenium
First Bug Love you
รูปภาพ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 99