0
1
00:00:04,700 --> 00:00:05,980
<v Volkan>In this lesson we are going to learn</v>
1

2
00:00:05,980 --> 00:00:08,030
how we can use open file dialogue.
2

3
00:00:08,030 --> 00:00:10,730
To do that what I will do, I will create a text editor
3

4
00:00:10,730 --> 00:00:13,390
and also buttons for this application.
4

5
00:00:13,390 --> 00:00:14,223
Let's get started.
5

6
00:00:14,223 --> 00:00:16,540
Here, what I will say, I will create a variable
6

7
00:00:16,540 --> 00:00:19,723
for my text editor and I will name it text editor.
7

8
00:00:23,940 --> 00:00:27,270
Is equal to I will say Text, a constructor method
8

9
00:00:27,270 --> 00:00:29,882
and here I need to say, what I need to say?
9

10
00:00:29,882 --> 00:00:34,040
I need to say root and second parameter will be width,
10

11
00:00:34,040 --> 00:00:36,430
and other one will be height.
11

12
00:00:36,430 --> 00:00:39,750
Height is equal to 15 okay.
12

13
00:00:39,750 --> 00:00:42,130
And what's next, what I need to do?
13

14
00:00:42,130 --> 00:00:45,330
I also need to use pack geometry manager for my text editor.
14

15
00:00:45,330 --> 00:00:46,880
Right, text_editor.pack
15

16
00:00:49,230 --> 00:00:51,040
and now our text editor is ready.
16

17
00:00:51,040 --> 00:00:55,430
Also I want to create a button for open file, okay.
17

18
00:00:55,430 --> 00:00:58,170
Button is equal to I will use button constructor method
18

19
00:00:58,170 --> 00:01:00,304
here and for the first parameter
19

20
00:01:00,304 --> 00:01:03,233
I will pass in root for text,
20

21
00:01:05,040 --> 00:01:08,770
let's say open, and also command,
21

22
00:01:08,770 --> 00:01:11,350
let's use command property
22

23
00:01:11,350 --> 00:01:15,220
and I'll say here open file, okay.
23

24
00:01:15,220 --> 00:01:17,530
This is our function name, definition name.
24

25
00:01:17,530 --> 00:01:19,149
Here what you need to do,
25

26
00:01:19,149 --> 00:01:20,540
you need to create this definition right?
26

27
00:01:20,540 --> 00:01:22,980
First I will say def openFile
27

28
00:01:25,920 --> 00:01:29,250
and here, firstly I will say pass, okay?
28

29
00:01:29,250 --> 00:01:32,210
And now we can run our application.
29

30
00:01:32,210 --> 00:01:33,450
Let's see it.
30

31
00:01:33,450 --> 00:01:37,900
If I run my application, as you see now it's here.
31

32
00:01:37,900 --> 00:01:39,600
only text editor is here
32

33
00:01:39,600 --> 00:01:41,700
because we didn't use Pack Geometry Manager
33

34
00:01:41,700 --> 00:01:42,810
for our button, right.
34

35
00:01:42,810 --> 00:01:45,850
Let's also use Pack Geometry Manager for our button.
35

36
00:01:45,850 --> 00:01:47,530
Button, pack.
36

37
00:01:47,530 --> 00:01:51,730
And here, if I run it again, as you see now
37

38
00:01:51,730 --> 00:01:54,860
my button is also here, and when click.
38

39
00:01:54,860 --> 00:01:58,670
I will run this definition, right, openFile definition.
39

40
00:01:58,670 --> 00:02:02,140
So what we need to do, to use openFile dialogue,
40

41
00:02:02,140 --> 00:02:05,310
before using it, we need to import something,
41

42
00:02:05,310 --> 00:02:10,310
we need to say here from tkinter import filedialog,
42

43
00:02:10,728 --> 00:02:13,200
and here what I will do.
43

44
00:02:13,200 --> 00:02:17,558
Firstly, I need to create our filename, okay.
44

45
00:02:17,558 --> 00:02:20,540
I will create a variable, it will be our filename,
45

46
00:02:20,540 --> 00:02:24,994
and with this filename we can create, we will create
46

47
00:02:24,994 --> 00:02:27,244
our openFile dialogue window.
47

48
00:02:28,670 --> 00:02:30,816
And here what I need to say,
48

49
00:02:30,816 --> 00:02:32,450
I need to say file_name is equal to
49

50
00:02:32,450 --> 00:02:34,937
filedialog.askopenfilename, okay.
50

51
00:02:37,990 --> 00:02:40,930
There are some functions about filedialog,
51

52
00:02:40,930 --> 00:02:43,560
but we will use askopenfilename, okay.
52

53
00:02:43,560 --> 00:02:46,275
And now here we need to paste in some parameters,
53

54
00:02:46,275 --> 00:02:51,275
and first one will be initial directory, okay,
54

55
00:02:51,689 --> 00:02:53,870
initialdir for initial directory.
55

56
00:02:53,870 --> 00:02:55,920
And I want to use my project directory,
56

57
00:02:55,920 --> 00:02:58,930
that's why I will say here single-quotes
57

58
00:02:58,930 --> 00:03:00,860
and of that slash, okay,
58

59
00:03:00,860 --> 00:03:03,750
and the second parameter will be my title,
59

60
00:03:03,750 --> 00:03:07,730
my window title, okay, and here I will say for title
60

61
00:03:07,730 --> 00:03:11,010
Select a File, okay.
61

62
00:03:11,010 --> 00:03:15,662
And other one, let's press enter here,
62

63
00:03:15,662 --> 00:03:16,912
and I will say filetypes;
63

64
00:03:20,570 --> 00:03:22,280
filetypes is something important here,
64

65
00:03:22,280 --> 00:03:23,927
now I will explain it.
65

66
00:03:23,927 --> 00:03:27,240
First we need to use parentheses here, okay,
66

67
00:03:27,240 --> 00:03:30,170
and for this editor, my text editor,
67

68
00:03:30,170 --> 00:03:33,310
I want to open txt files, okay.
68

69
00:03:33,310 --> 00:03:38,250
And of course it can be a jpeg, images, files,
69

70
00:03:38,250 --> 00:03:43,040
or another file types, but for this application,
70

71
00:03:43,040 --> 00:03:47,600
and for our text editor, we need to open txt files.
71

72
00:03:47,600 --> 00:03:51,732
And here I will say Txt files, okay.
72

73
00:03:51,732 --> 00:03:55,913
And my second parameter will be here, I need to say,
73

74
00:03:56,947 --> 00:04:00,700
here extension .txt, right,
74

75
00:04:00,700 --> 00:04:04,930
this is extension for my txt files, for notepad also.
75

76
00:04:04,930 --> 00:04:07,050
And other one, what is next?
76

77
00:04:07,050 --> 00:04:09,160
And here, I will use another parameter,
77

78
00:04:09,160 --> 00:04:10,110
and here I will say
78

79
00:04:14,410 --> 00:04:17,790
All files, okay.
79

80
00:04:17,790 --> 00:04:21,220
After all files, what I need to do, I need to use here
80

81
00:04:22,160 --> 00:04:24,920
asterisk dot asterisk, okay.
81

82
00:04:24,920 --> 00:04:28,455
If I ran my, asterisk dot asterisk,
82

83
00:04:28,455 --> 00:04:31,670
if I ran my application now, here something wrong,
83

84
00:04:31,670 --> 00:04:34,593
we need to use quotes here, and here also.
84

85
00:04:36,010 --> 00:04:37,900
Double or single, it doesn't matter.
85

86
00:04:37,900 --> 00:04:41,043
Something again wrong, because one quote.
86

87
00:04:43,790 --> 00:04:45,113
Let me remove this one.
87

88
00:04:48,020 --> 00:04:48,953
Quote, end quote.
88

89
00:04:51,457 --> 00:04:53,210
Where there is something, why is like that?
89

90
00:04:53,210 --> 00:04:56,613
And now, here I will say asterisk dot asterisk.
90

91
00:05:00,881 --> 00:05:02,890
Maybe it's because now here we used, already we used,
91

92
00:05:02,890 --> 00:05:05,660
sorry here I need to use another quote,
92

93
00:05:05,660 --> 00:05:08,800
right, that one, and here let's use double quotes,
93

94
00:05:08,800 --> 00:05:10,210
it's better.
94

95
00:05:10,210 --> 00:05:15,210
And remove these guys, asterisk dot asterisk, okay.
95

96
00:05:15,970 --> 00:05:17,483
If I run my application now,
96

97
00:05:19,454 --> 00:05:21,460
and see now, our button is also here.
97

98
00:05:21,460 --> 00:05:23,550
If I click, if I hit this open button,
98

99
00:05:23,550 --> 00:05:24,510
let's check it.
99

100
00:05:24,510 --> 00:05:27,510
As you see now, this is our dialogue window,
100

101
00:05:27,510 --> 00:05:29,220
and let's talk about it.
101

102
00:05:29,220 --> 00:05:32,050
This is our project directory, by the way,
102

103
00:05:32,050 --> 00:05:34,795
this is initial directory, our project area,
103

104
00:05:34,795 --> 00:05:38,640
Select a file is our title, it's here,
104

105
00:05:38,640 --> 00:05:42,140
and the file types, as you see, it says txt files.
105

106
00:05:42,140 --> 00:05:44,700
And of course, also we have another parameter,
106

107
00:05:44,700 --> 00:05:46,300
it says All files.
107

108
00:05:46,300 --> 00:05:49,131
If I click this, you see it's like
108

109
00:05:49,131 --> 00:05:52,780
All files is here and Txt files is here.
109

110
00:05:52,780 --> 00:05:57,780
But now, with this code we only created this window, okay.
110

111
00:05:58,510 --> 00:06:00,243
Now what we need to do,
111

112
00:06:00,243 --> 00:06:02,493
we need to create something
112

113
00:06:03,348 --> 00:06:05,884
for content of our file,
113

114
00:06:05,884 --> 00:06:07,834
we need to create something else.
114

115
00:06:07,834 --> 00:06:10,667
Here what I need to say, and here,
115

116
00:06:12,513 --> 00:06:13,820
I need to come here, and I will say,
116

117
00:06:13,820 --> 00:06:16,580
create a variable and its name, it will be content.
117

118
00:06:16,580 --> 00:06:18,040
Content is equal to, I will say,
118

119
00:06:18,040 --> 00:06:21,080
I need to use here open function, okay.
119

120
00:06:21,080 --> 00:06:23,347
For the open function I will say file_name,
120

121
00:06:24,260 --> 00:06:25,640
open my file, okay.
121

122
00:06:25,640 --> 00:06:26,630
And after that what I need to do,
122

123
00:06:26,630 --> 00:06:27,710
I need to read it, okay.
123

124
00:06:27,710 --> 00:06:32,710
I will say read, and now we already read our file,
124

125
00:06:32,710 --> 00:06:33,870
but what we need to do,
125

126
00:06:33,870 --> 00:06:37,510
now I want to insert this content or file
126

127
00:06:37,510 --> 00:06:38,470
to my text editor.
127

128
00:06:38,470 --> 00:06:39,730
To do that, what I will say, I will say
128

129
00:06:39,730 --> 00:06:42,160
text editor.insert,
129

130
00:06:42,160 --> 00:06:44,530
and here the first parameter will be END
130

131
00:06:44,530 --> 00:06:48,100
and second one will by my content, okay.
131

132
00:06:48,100 --> 00:06:50,323
And if I run my application again, Run.
132

133
00:06:51,440 --> 00:06:56,440
Now it's open, and here this one is, for example, document.
133

134
00:06:57,000 --> 00:06:58,550
Okay, I will click this one.
134

135
00:06:58,550 --> 00:07:00,990
As you see now, our content is here,
135

136
00:07:00,990 --> 00:07:03,910
and inserted to our text editor.
136

137
00:07:03,910 --> 00:07:05,520
This is how we can use filedialog.
137

138
00:07:05,520 --> 00:07:07,520
For now that's all, see you in the next.
